Home > Web Front-end > JS Tutorial > Solution to the failure of checkbox selection in jquery_jquery

Solution to the failure of checkbox selection in jquery_jquery

WBOY
Release: 2016-05-16 16:24:11
Original
920 people have browsed it

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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template