jQuery: Single button click, click event firing two or more times

前端 未结 8 1587
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 13:45

I have HTML input and button elements. I use the buttons to submit forms, open secondary windows, etc. The problem is, a single click is turning into 2 (and sometimes many more

相关标签:
8条回答
  • 2021-01-30 14:46

    for me it was caused by

    $(function() {
        $('.some-element').click(function() {
            //event that fires twice
        });
    
        //some exception-throwing code here
    });
    

    jQuery was calling the anonymous ready function twice(?!) because of the exception so the click function would be bound twice.

    0 讨论(0)
  • 2021-01-30 14:46

    I had a problem with modal dialog windows. The event fired once for each time I had opened a dialog in the session. No problem on the first window, fired twice on the second window, fired three times on the third window, etc.

    I found that the initialization function for the dialog was being called each time I opened a new dialog window in the same session. I added a global variable notInit = -1 and assigned the value to 1 when running init. I checked the value of notInit before running the init function and only proceeded with init if the value was -1.

    0 讨论(0)
提交回复
热议问题