JQuery-函数一次激活一次

JQuery-函数一次激活一次,jquery,Jquery,我在JQuery函数中编写,它可以选择或取消选择页面上的所有复选框。我有组复选框 <input type="checkbox" title="111" value='111' name="cid[]" id='cb1'/> ... <input type="checkbox" title="nnn" value='nnn' name="cid[]" id='cbn'/> 我使用Firefox22.0。。。 当我单击“全选”复选框时,此函数选中了“全选”复选框

我在JQuery函数中编写,它可以选择或取消选择页面上的所有复选框。我有组复选框

  <input type="checkbox" title="111" value='111' name="cid[]" id='cb1'/>
  ...
  <input type="checkbox" title="nnn" value='nnn' name="cid[]" id='cbn'/>
我使用Firefox22.0。。。 当我单击“全选”复选框时,此函数选中了“全选”复选框。在此之后,我再次单击“CheckAll”复选框。结果-所有复选框均未选中。但是当我第二次尝试选中所有复选框时,这并没有发生。为什么?

使用
.prop()
而不是
.attr()

从jQuery1.6开始,对于具有 未设置。若要检索和更改DOM属性,如选中,
选中或禁用表单元素的状态,请使用.prop()方法。

它工作正常。请参见此处FWIW:
$(this).attr('id')
编写
this.id
,而
if(this.checked==true)
基本上就是编写
if(this.checked)
非常奇怪。但您的示例在我的浏览器上也不起作用。
<input type="checkbox" id="CheckAll" title="Choose all" value="" name="checkall-toggle"/>
$(":checkbox").click(function(){
    var id = $(this).attr('id');
    if (id=='CheckAll')
    {
        if (this.checked==true)
        {

            $('INPUT[type=\'checkbox\']').attr('checked', true);

        }else
        {
            $('INPUT[type=\'checkbox\']').attr('checked', false);
        }
    }
});
$(":checkbox").click(function () {
    var id = $(this).attr('id');
    if (id == 'CheckAll') {
        if (this.checked == true) {
            $('INPUT[type=\'checkbox\']').prop('checked', true);
        } else {
            $('INPUT[type=\'checkbox\']').prop('checked', false);
        }
    }
});