如何使用jQuery重新加载页面时如何保持复选框状态

时间:2019-03-19 11:18:20

标签: jquery checkbox state checked

我有一个复选框,为它定义了两个状态,当我重新加载页面时,此复选框恢复为默认状态(或未选中)。我在论坛上遇到了麻烦,但没有得到答案。这是我的代码

    $('#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
            }
        });
    }

0 个答案:

没有答案
相关问题