attr('checked', true)设置状态只有第一次有用,再次点击就无效果。类似的属性还有selected 和 disabled。当遇到这种情况的时候,把attr换成prop方法,prop和attr一样使用,以下有例子;
全选的小例子:
$(function(){ $("#j_cbAll").click(function(){ $("#j_tb input").prop("checked",$(this).prop("checked")) //根据全选按钮的状态,设置下属按钮的状态。 }) $("#j_tb input").click(function(){ //当点击下属产品时,全选按钮发生改变,此处用下属产品input的长度来判断是否为选中状态 var inputLen=$("#j_tb input").length; var inputCheckLen=$("#j_tb input:checked").length; if(inputLen==inputCheckLen){ $("#j_cbAll").prop("checked",true) }else{ $("#j_cbAll").prop("checked",false) } }) })
![b61e4b2f46598c3a81775a7ed38bde80.png](https://img-blog.csdnimg.cn/img_convert/b61e4b2f46598c3a81775a7ed38bde80.png)