开发者

jQuery live confirm

I'm having problems with .live() and confirm. It's multiplying the confirm dialogs for every click. I know about .die() but i can't get it to work.

$("button.del").live("click", function(){

        if(!confirm("Are you sure?")) {
            //close
        }

    });

I've tried $("button.del").die("click"); righ开发者_如何学Got after the above code, in which case the confim doesn't even fire.


Does the dialog box appear multiple times if you just run that code by itself?

If the dialog box is appearing multiple times, one likely explanation is that you are accidentally running this .live() binding more than once. If that happened, you would see one dialog box for each time you bound an event to the button.

Make sure you are only attaching this function to the button once.

If you take a look at this standalone example, you can see that your code is fine.


Can you post the HTML as well.

One cause I can speculate for this is that the .del class is specified into some child class, and the event is firing on both parent and child. This would happen for the following:

<div class="testclass">
    test
    <div class="testclass">
        test2
    </div>
</div>

...

$(".testclass").click(function() { alert("test"); });

Another reason would be if you accidentally bound it twice, i.e. the following would cause the same problem

$(".testclass").click(function() { alert("test"); });
$(".testclass").click(function() { alert("test"); });

We really need to see more of your code. You must utilise live for a reason. Do you get the same result with a simple click() binding?


Thank you all for your replies...turn out it was a bug in my code... Sorry...

I didn't see it... the part of the code with confirm was reloading on every click...hence the multiplying...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜