JQuery選中checkbox方法代碼執行個體(全選、反選、全不選),jquerycheckbox

來源:互聯網
上載者:User

JQuery選中checkbox方法代碼執行個體(全選、反選、全不選),jquerycheckbox

1、checkbox list選擇

代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">  <title></title>  <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>  <script type="text/javascript">    $(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(","));      });    });  </script></head><body>  <div>    <input name="chkItem" type="checkbox" value="今日話題" />今日話題    <input name="chkItem" type="checkbox" value="視覺焦點" />視覺焦點    <input name="chkItem" type="checkbox" value="財經" />財經    <input name="chkItem" type="checkbox" value="汽車" />汽車    <input name="chkItem" type="checkbox" value="科技" />科技    <input name="chkItem" type="checkbox" value="房產" />房產    <input name="chkItem" type="checkbox" value="旅遊" />旅遊  </div>  <div>    <input id="btnCheckAll" type="button" value="全選" />    <input id="btnCheckNone" type="button" value="全不選" />    <input id="btnCheckReverse" type="button" value="反選" />    <input id="btnSubmit" type="button" value="提交" />  </div></body></html>

2、checkbox table選中

:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">  <title></title>  <style type="text/css">    table    {      border-collapse: collapse;    }    td    {      border: 1px solid #ccc;    }  </style>  <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>  <script type="text/javascript">    $(function () {      // chkAll全選事件      $("#chkAll").bind("click", function () {        $("[name = chkItem]:checkbox").attr("checked", this.checked);      });       // chkItem事件      $("[name = chkItem]:checkbox").bind("click", function () {        var $chk = $("[name = chkItem]:checkbox");        $("#chkAll").attr("checked", $chk.length == $chk.filter(":checked").length);      })    });  </script></head><body>  <table id="tb">    <thead>      <tr>        <td>          <input id="chkAll" type="checkbox" />        </td>        <td>          分類名稱        </td>      </tr>    </thead>    <tbody>      <tr>        <td>          <input name="chkItem" type="checkbox" value="今日話題" />        </td>        <td>          今日話題        </td>      </tr>      <tr>        <td>          <input name="chkItem" type="checkbox" value="視覺焦點" />        </td>        <td>          視覺焦點        </td>      </tr>      <tr>        <td>          <input name="chkItem" type="checkbox" value="財經" />        </td>        <td>          財經        </td>      </tr>      <tr>        <td>          <input name="chkItem" type="checkbox" value="汽車" />        </td>        <td>          汽車        </td>      </tr>      <tr>        <td>          <input name="chkItem" type="checkbox" value="科技" />        </td>        <td>          科技        </td>      </tr>      <tr>        <td>          <input name="chkItem" type="checkbox" value="房產" />        </td>        <td>          房產        </td>      </tr>      <tr>        <td>          <input name="chkItem" type="checkbox" value="旅遊" />        </td>        <td>          旅遊        </td>      </tr>    </tbody>  </table></body></html>


聯繫我們

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