为什么我的函数调用很多次,使用bootstrap模式?

时间:2014-07-28 18:07:01

标签: modal-dialog typing

我的JS:

$("#modalADDpp").on('shown',function(e){
    $(this).find('input[type=text]').filter(':first').focus();
    var count = 0;
    alert(count);
    $('#pp_tab a').click(function () {
        $(this).tab('show');
    })
    $("#TenPP").typing({ // jquery for check when finish typing
        start: function(event ,$elem) {
           $(".add-on").remove();
           return false;   
        },
        stop: function (event, $elem) {
            count ++;
            alert(count);
            if(count == 1 )
            {
                Ajax_Check_Name($elem);// Check name same with database by ajax
            }       
            return false;
        },
        delay: 500
    });
    $("#modalADDpp").on('hidden',function(e){
        $(this).closest('form').find("input[type=text], textarea").val("");
        $(".add-on").remove();
    });
});

我的模态中显示$ count = 0。第一次,它工作正常。但是当隐藏模态并重新打开时,$ count not = 0,$ count now = 2 =>我的功能将调用两次,如果再次关闭,则加上3次,4次,5次。

我用

  

if(count == 1)

因为我希望它只运行一次。所以它工作正常,但我不知道问题出在哪里。需要帮助!!!

输入代码.js

(function ($) {

    //--------------------
    //  jQuery extension
    //--------------------

    $.fn.typing = function (options) {
        return this.each(function (i, elem) {
            listenToTyping(elem, options);
        });
    };


    //-------------------
    //  actual function
    //-------------------

    function listenToTyping(elem, options) {
        // override default settings
        var settings = $.extend({
            start: null,
            stop: null,
            delay: 400
        }, options);

        // create other function-scope variables
        var $elem = $(elem),
            typing = false,
            delayedCallback;

        // start typing
        function startTyping(event) {
            if (!typing) {
                // set flag and run callback
                typing = true;
                if (settings.start) {
                    settings.start(event, $elem);
                }
            }
        }

        // stop typing
        function stopTyping(event, delay) {
            if (typing) {
                // discard previous delayed callback and create new one
                clearTimeout(delayedCallback);
                delayedCallback = setTimeout(function () {
                    // set flag and run callback
                    typing = false;
                    if (settings.stop) {
                        settings.stop(event, $elem);
                    }
                }, delay >= 0 ? delay : settings.delay);
            }
        }

        // listen to regular keypresses
        $elem.keypress(startTyping);

        // listen to backspace and delete presses
        $elem.keydown(function (event) {
            if (event.keyCode === 8 || event.keyCode === 46) {
                startTyping(event);
            }
        });

        // listen to keyups
        //$elem.keyup(stopTyping);

        // listen to blurs
        $elem.blur(function (event) {
            stopTyping(event, 0);
        });
    }
})(jQuery);

1 个答案:

答案 0 :(得分:0)

尝试将计数重置为0 ...隐藏或停止功能......

if(count == 1 )
{
   Ajax_Check_Name($elem);// Check name same with database by ajax
   count = 0;
} 

$("#modalADDpp").on('hidden',function(e){
        $(this).closest('form').find("input[type=text], textarea").val("");
        $(".add-on").remove();
        count = 0;
});