jquery checkbox all selected failure solution

  • 2020-05-10 17:37:07
  • OfStack

If you use jQuery 1.6, code if ($(elem).attr(" checked ")) will get a property (attribute) that does not change when the checkbox is selected and checked. It is only used to store the initial value of the default or selected property. To maintain backward compatibility, the.attr () method updates the property property, in addition to returning the property value, starting with jQuery 1.6.1 +, so boolean attribute (Boolean property) does not need to change its value through.prop (). It is recommended to use 1 of the above method to obtain the value of checked.

Using jQuery's attr method to get and set the "checked" property of the check box, it was found that the first time to select all/unselect all was valid, and then it was invalid. However, if you look at the html source file, the check box property has indeed been updated, that is, there is no update in the page. 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> Future generations </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>

Related articles: