jquery .click being called multiple times

前端 未结 8 2246
小蘑菇
小蘑菇 2021-02-20 11:12

I am getting unexpected results with jQuery trying to set the \"click\" method of a div. Please see this jsfiddle. Be sure to open the console window. Click the word a few times

8条回答
  •  太阳男子
    2021-02-20 11:21

    The same problem was happening with me . Joseph Erickson's answer about unbind() works. If it helps , here is the summary of how onclick event ended up being called multiple times in my code :

    • AJAX call is updating a table's tbody. The tbody content is totally dynamic.
    • The table rows are retrieved batch-wise (via server side catching).
    • A specific table data(td) had a hyperlink that has a click event associated to it.(click event added as part of AJAX callback function)
    • Now, every time, the AJAX gets called, one click event gets associated to the td.
    • Consequently, as many times, a AJAX is invoked, that many click handlers got attached to the td.

    I solved this as per suggestion given by Joseph Erickson. Within the AJAX callback, I do this now : $(".msgLinkDiv").unbind(); (".msgLinkDiv").click(function(e){do my stuff});

提交回复
热议问题