我有一个存储在cookie中的复选框。
下面是jquery代码:
//JQuery that will set the checkbox in it's current state
$("#checkAll").on("change", function() {
$(':checkbox').not(this).prop('checked', this.checked);
});
$(":checkbox").on("change", function(){
var checkboxValues = {};
$(":checkbox").each(function(){
checkboxValues[this.id] = this.checked;
});
$.cookie('checkboxValues', checkboxValues, { expires: 7, path: '/' })
});
function repopulateCheckboxes(){
var checkboxValues = $.cookie('checkboxValues');
if(checkboxValues){
Object.keys(checkboxValues).forEach(function(element) {
var checked = checkboxValues[element];
$("#" + element).prop('checked', checked);
});
}
}
$.cookie.json = true;
repopulateCheckboxes();
上面的代码运行良好,但当我尝试使用按钮取消选中它们时,它根本不会取消选中。
下面是我使用的函数:
$("#UncheckAll").click(function(){
$("input[type='checkbox']").prop('checked',false);
});
我该怎么办?谁能帮帮我。
发布于 2014-04-08 03:11:13
为所有复选框指定一个类,如myclass。然后使用jQuery。
在document.ready
函数中删除cookie
$.cookie("checkboxValues", null);//checkboxValues is the name of your cookie
对于jQuery 1.6+
$("#UncheckAll").click(function(){
$('input:checkbox:checked.myclass').prop('checked',false);
});
对于jquery 1.5-
$("#UncheckAll").click(function(){
$('input:checkbox:checked.myclass').attr('checked',false);
});
发布于 2014-04-08 03:08:07
尝尝这个
$("#UncheckAll").click(function(){
$('input:checkbox.class').prop('checked',false);
});
或者这个如果没有公共类的话
$("#UncheckAll").click(function(){
$(':checkbox').prop('checked',false);
});
https://stackoverflow.com/questions/22920849
复制相似问题