Y.G.J
Y.G.J

Reputation: 1108

jQuery live click fires more than once

I have this code for ajax a page on search click:

$.ajax({
    url: "ClientsList.asp",
    type: "POST",
    data: "name=" + Name + "&org=" + Org + "&job=" + Job + "&type=" + Type,
    success: function(msg){
        $("#SRP").html(msg);
        $("#Loading").fadeOut("noraml",function(){
            $("#SRP").fadeIn();
        });

    }
});

then in the content loaded I have checkboxes that I need to catch, so I do that with this code

$(".SearchResultSelectBox").live("click", function(event) {
    $(this).is(":checked") ? DoCheckSelect($(this).attr("rel")) : unDoCheckSelect($(this).attr("rel"));
});

The first time I search and get the resulted page from the ajax the checkbox work great. the second time I use the search, every live click I have is doubled. the third time I use the ajax, every live click is tripled and so on...

I tried "return false" but then it work but the checkbox doesn't get clicked

What can be done?

NEVER MIND! I had a mistake, I had the second set of function inside of the first one so every time I made the search function I made the inside function for live click again

Upvotes: 2

Views: 995

Answers (2)

Peeter
Peeter

Reputation: 9382

Try

event.stopPropagation();

$(".SearchResultSelectBox").live("click", function(event) {
    $(this).is(":checked") ? DoCheckSelect($(this).attr("rel")) : unDoCheckSelect($(this).attr("rel"));
    event.stopPropagation();
});

Upvotes: 1

Andrei Andrushkevich
Andrei Andrushkevich

Reputation: 9973

may be try to use

unbind('click');

and bind it again for each ajax request

Upvotes: 1

Related Questions