简体   繁体   English

页面重新加载/刷新ajax后保持复选框状态

[英]Keep checkbox status after page reload/refresh ajax

I am working around a script that keeps one or multiple checkbox values checked on page reloaded or refreshed.我正在处理一个脚本,该脚本在重新加载或刷新的页面上检查一个或多个复选框值。 The code below was executed to maintain the checkboxes values but it's not working.执行下面的代码以维护复选框值,但它不起作用。 Any help will be appreciated.任何帮助将不胜感激。

<script>
            function onClickBox() {
                let checked=$("#check").is(":checked");
                localStorage.setItem("checked", checked);

            }
            function onReady() {

                let checked="true"==localStorage.getItem("checked");
                $("#check").prop('checked', checked);
                $("#check").click(onClickBox);
            }
            $(document).ready(onReady);

</script>

This line of code return '<input type="checkbox" id="check" href="#"' + 'order_id="'+ data + '">Yes</>';这行代码return '<input type="checkbox" id="check" href="#"' + 'order_id="'+ data + '">Yes</>'; allows admin to check a checkbox, after which the code takes the ID and then runs the check_payment.php that has the query that inserts yes to that ID row.允许管理员选中一个复选框,然后代码获取 ID,然后运行 ​​check_payment.php,其中包含向该 ID 行插入 yes 的查询。

 aoColumnDefs: [
           {
                aTargets: [6],
                mData: "userId",
                mRender: function (data, type, full) {
                     return '<input type="checkbox"  id="check" href="#"' + 'order_id="'+ data + '">Yes</>';
                    //return '<button href="#"' + 'order_id="'+ data + '">Yes</button>';
                }

            }
         ],
         language: {
            url: 'https://cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/English.json'
            }
});
$('#example').on( 'click', 'input:checkbox', function () {
            var data = table.row( $(this).parents('tr') ).data();
            var order_id = data['order_id'];
            console.log(order_id);
            $.ajax({
                type: "POST",
                url: 'check_payment.php',
                data: "order_id=" + order_id,   
            });
        } );

Here's a VanillaJS approach.这是一个 VanillaJS 方法。 We have document.querySelector now, so in my view, jQuery has been depreciated for a while.我们现在有了document.querySelector ,所以在看来,jQuery 已经贬值了一段时间。 As far as I can tell your logic is solid, so I can only assume it's one of the jQuery calls that's breaking, and I honestly don't know which one.据我所知,您的逻辑是可靠的,所以我只能假设它是中断的 jQuery 调用之一,老实说,我不知道是哪一个。

// get element #check
var checkbox = window.check;

// checking and setting
isChecked = () => localStorage.getItem("checked") == "true";
setCheck = (v) => checkbox.checked = v;

// event handlers
onClickBox = () => localStorage.setItem("checked", checkbox.checked);
onReady = () => setCheck(isChecked());

// bindings
checkbox.addEventListener("click", onClickBox);
document.addEventListener("load", onReady);

Edit: I think what might be happening is the onLoad handler (or onReady, with jQuery) is never firing because the code is embedded in the body.编辑:我认为可能发生的事情是 onLoad 处理程序(或 onReady,使用 jQuery)永远不会触发,因为代码嵌入在主体中。 If the script is in the head, use the eventHandler;如果脚本在头部,则使用 eventHandler; if the script is in the body, just call onReady() after setting the click handler for the checkbox.如果脚本在正文中,只需在为复选框设置点击处理程序后调用onReady()即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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