If you use jQuery 1.6, the code if ($(elem).attr("checked")) will get an attribute that does not change whether the checkbox is selected or selected. It is only used to store the initial value of the default or selected property. In order to maintain backward compatibility, the .attr() method starting from jQuery 1.6.1 will also update the property attribute in addition to returning the attribute value, so the boolean attribute does not need to change its value through .prop(). It is recommended to use one of the above methods to obtain the value of checked.
Use jQuery's attr method to get and set the "checked" attribute of the check box. It is found that selecting/unselecting all is effective for the first time, and then it is invalid. However, looking at the html source file, the check box attribute has indeed been It has been updated, but the page is not updated. The correct method is as follows:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script><script type="text/javascript">// <![CDATA[ $(function(){ $('.ckAll').click(function(){ $(".box-items").each(function(){ $(this).prop("checked",!!$(".box-all").prop("checked")); }); }); }); // ]]></script> <div><label class="ckAll"><input class="box-all" type="checkbox" /><span>全选</span></label> <input class="box-items" type="checkbox" /> <input class="box-items" type="checkbox" /> <input class="box-items" type="checkbox" /> <input class="box-items" type="checkbox" /> <input class="box-items" type="checkbox" /> </div>