小编Anu*_*pta的帖子

页面刷新后如何保持复选框处于选中状态

因此,我尝试在页面刷新后保持选中复选框,而我正在使用以下脚本,但是当页面刷新时,会将所有其他复选框选中。 还请注意,输入是动态生成的,不能用硬编码编码,所以我不能将不同的ID输入不同的输入

jQuery(function(){
    var test = localStorage.input === 'true'? true: false;
    jQuery('input').prop('checked', test || false);
});

jQuery('input').on('change', function() {
    localStorage.input = jQuery(this).is(':checked');
    console.log(jQuery(this).is(':checked'));
});
Run Code Online (Sandbox Code Playgroud)

考虑以下复选框在代码中

<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
Run Code Online (Sandbox Code Playgroud)

html javascript jquery input

-1
推荐指数
1
解决办法
1019
查看次数

标签 统计

html ×1

input ×1

javascript ×1

jquery ×1