Click event doesn't fire after ajax event

I have a checkbox styled using iCheck. This parts works fine. My problem is that i want to save the checkbox state in my database on every change. But my ajax request does only fire the first time.

I have a setup like this:

HTML:

<ul class='tasks'>
   <li><input type=checkbox rel='module' data='1' name=done[]  class='taskStatus'>Text</li>
</ul>

jQuery

$("input[type=checkbox]").on("ifClicked", function(){
    alert("init");

    var stateObj = {
        id: $(this).attr("data"),
        mode: $(this).attr("rel"),
        state: null
    };

    if($(this).prop("checked") === false){
        stateObj.state = 1;
    } else {
        stateObj.state = 0;
    }

    updateState(stateObj);
});


function updateState(stateObj){
    alert("updating");

    $.ajax({
        url: "moduleAjax.php?module=" + stateObj.mode + "&page=eventState",
        type: "POST",
        data: "obj=" + JSON.stringify(stateObj), 
        success: function(data){
            alert(data);
        },
        error: function(a, b, c){
            alert("error");
        }
    });
}

I've tried to comment out the ajax request. Doing so the alert boxes is firing as they should.

I have a workaround adding window.location.reload(); after alert(data). But in my opinion this shouldn't be necessary. I've also tried sending the request in async: false; mode with no success. I'm out of ideas.

Question

Why isn't the ifClicked event fired on every click, only the first, when the ajax request is sent?

Additional info

I get no errors in the console when executing the script. Neither any alerts the second run.

I haven't even been able to reproduce the problem in jsfiddle.

Anyone with any clue?

Edit

I just tested with the new iCheck beta source code. then the events get fired as they should, But a few other problems where raised. So this seems to be deriving from a bug i iCheck. (But i could be wrong).


How about re-initializing your event handler after the Ajax response?

$(function () {
    initClickedHandler();
});

function initClickedHandler(){
    $("input[type=checkbox]").on("ifClicked", function(){
            alert("init");
            var stateObj = {
                id: $(this).attr("data"),
                mode: $(this).attr("rel"),
                state: null
            }

            if($(this).prop("checked") === false){
                stateObj.state = 1;
            }
            else{
                stateObj.state = 0;
            }
            updateState(stateObj)
    });
}


function updateState(stateObj){
    alert("updating");
    $.ajax({
        url: "moduleAjax.php?module=" + stateObj.mode + "&page=eventState",
        type: "POST",
        data: "obj=" + JSON.stringify(stateObj), 
        success: function(data){
            alert(data);
            initClickedHandler();
        },
        error: function(a, b, c){
            alert("error");
        }
    });
}

Also, you're missing a semi-colon after updateState(stateObj)


您可以通过使用jQuery的.click .click()方法(jsfiddle)完全避免这个问题:

$('input[type="checkbox"]').on('click', function() {

    var stateObj = {
        id: $(this).attr("data"),
        mode: $(this).attr("rel"),
        state: null
    }

    if( $(this).prop('checked') === false ) {
        stateObj.state = 1;
    } else {
        stateObj.state = 0;   
    }

    updateState(stateObj);
});

function updateState(stateObj) {
    console.log(stateObj.state);   
}
链接地址: http://www.djcxy.com/p/56022.html

上一篇: JQuery忽略click()和change()

下一篇: 点击事件不会在ajax事件后触发