mardi 19 mars 2019

how to keep checkbox state wehn page is reload with jquery

I have a checkbox, I've defined two state for it, and when I reload page, this checkbox back to the default state (or unchecked). I got in trouble at the forum, but I did not get my answer. This is my code

    $('#mycheckbox').change(function () {
        var returnVal = ("Are " + "you sure?");
        if (returnVal) {
            postToServer($(this).prop("checked"));

        } else {
            $(this).prop("checked", !$(this).is(":checked"));

        }
    });


    function postToServer(state) {
        let value = (state) ? 1 : 0;
        //alert('Posted Value: ' + value);
        $.ajax({
            type: 'POST',
            url: 'checkbox.php',
            data: {'value': +value},
            success: function (response) {
                //handle response
            }
        });
    }




Aucun commentaire:

Enregistrer un commentaire