jQuery checkbox選中問題之prop與attr注意點分析

來源:互聯網
上載者:User

標籤:官網   參考   space   his   key   function   reverse   false   checkbox   

$(function () {  // 全選  $("#btnCheckAll").bind("click", function () {    $("[name = chkItem]:checkbox").attr("checked", true);  });  // 全不選  $("#btnCheckNone").bind("click", function () {    $("[name = chkItem]:checkbox").attr("checked", false);  });  // 反選  $("#btnCheckReverse").bind("click", function () {    $("[name = chkItem]:checkbox").each(function () {      $(this).attr("checked", !$(this).attr("checked"));    });  });  // 全不選  $("#btnSubmit").bind("click", function () {    var result = new Array();    $("[name = chkItem]:checkbox").each(function () {      if ($(this).is(":checked")) {        result.push($(this).attr("value"));      }    });    alert(result.join(","));  });});

 

問題描述:第一次點全選可以,然後點擊全不選,接著再點擊全選、全不選、反選就沒了反應,後來用其他瀏覽器發下可以,所以感覺是相容性的問題,後來查閱資料發現果然是的,參考地址http://jquery.com/

解決方案:將attr換為prop即可,經過驗證各個瀏覽器都是好的,官網說明是在1.6之後建議用prop,在此記錄以備後用。

jQuery checkbox選中問題之prop與attr注意點分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.