Why is this jQuery ajax click event firing multiple times?

后端 未结 7 1871
无人共我
无人共我 2020-12-14 08:13

I have tried unbinding the click event, but it fires sometimes twice sometimes 5 times!! Getting a bit fed up now!

Code from modal.asp

$         


        
7条回答
  •  悲哀的现实
    2020-12-14 08:52

    You can fix your problem in two ways:

    1) Move your javascript code into main.asp, instead of modal.asp.

    This will instantly fix your double-fire issue. You would have to modify your code slightly to pull the WatchListID value from some HTML inside modal.asp, rather than coding it directly as you are (since main.asp won't have that value yet.)

    2) Keep your javascript code where it is, but stop using live events.

    Use a simple ".click(function () {" call, instead of ".live('click', function() {".

    You are reloading modal.asp each time the dialog opens, which means your javascript is being run every time it loads. If you keep your javascript code in modal.asp, then live() is probably not appropriate.

提交回复
热议问题