jQuery复选框选中所有并删除所有 [英] jQuery checkbox check all and remove all

查看:95
本文介绍了jQuery复选框选中所有并删除所有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我有这段代码非常适合检查和取消选中表单中的所有复选框.但是,如果您全部选中它们,然后都不选中它们,然后尝试再次全部选中,则不会发生任何事情.

Ok so i have this code that works great for checking and un-checking all of the checkboxes in a form. But if you check them all then check none of them then try to check all again nothing happens.

该如何解决?

$('#yearall').click(function() {
    var c = this.checked;
    $('.year:checkbox').prop('checked',c);
});
$('#yearNone').click(function() {
    var c = this.checked;
    $('.year:checkbox, #yearall').removeProp('checked',c);
});

我在这里做了一个小提琴 http://jsfiddle.net/nf75M/

I have greated a fiddle here http://jsfiddle.net/nf75M/

推荐答案

更改

$('#yearNone').click(function() {
    var c = this.checked;
    $('.year:checkbox, #yearall').removeProp('checked',c);
});

$('#yearNone').click(function() {
    $('.year:checkbox, #yearall').prop('checked',false);
});

删除属性后,就不能再在#yearAll点击处理程序中进行设置.

Once you remove the property, you can no longer set it in the #yearAll click handler.

FIDDLE

这篇关于jQuery复选框选中所有并删除所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆