這篇文章主要介紹了jquery中checkbox全選失效的解決方法,需要的朋友可以參考下
如果你使用jQuery 1.6 ,代碼if ( $(elem).attr(“checked”) ),將獲得一個屬性(attribute) ,它不改變該復選框被選中和選中。它只是用來存儲默認或選中屬性的初始值。為了保持向后兼容,.attr() 方法從 jQuery 1.6.1+ 開始除了返回屬性值外,還會更新 property 屬性,因此 boolean attribute(布爾屬性)不需要通過 .prop() 來改變其值。推薦使用上述方法之一,來取得 checked 的值。
使用jQuery的attr方法獲取和設置復選框的”checked”屬性,發現第一次全選/取消全選有效,之后就無效了,但查看html源文件,復選框屬性確實已經被更新了,就是頁面中沒有更新,正確的方法如下:
- <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>
新聞熱點
疑難解答
圖片精選