jquery中checkbox全選失效的解決方案

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   使用   sp   java   on   

如果你使用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>

 

jquery中checkbox全選失效的解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.