简体   繁体   中英

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)

You might be able to avoid this issue altogether by using jQuery's .click() method ( 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);   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM