欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

jquery 当某个复选框选中的时候 下面的 子复选框同时选中 全部选中 checkbox 全选 toggle 切换开关 有大用 有大大用 有大大大用

下面是自己亲自做的

<input type="checkbox" onclick="setCheck(this)">

function setCheck(btn){

    $(btn).parents('.bg-info').next('ul').find('input').prop('checked', btn.checked);
}




jQuery - toggle select all checkboxes

Ask QuestionAsked Modified 8 years, 8 months agoViewed 47k times25

Fought with a bunch of examples and, being still new to jQuery/Javascript, cannot get my code to function (here my my template in gsp):

<table>
    <thead>
    <tr>
       <th><input type="checkbox" name="selectAll" onclick="selectAll(this.checked);"/></th>
    </tr>
    </thead>
    <tbody>
         <td>
            <td><input type="checkbox" name="domainList" value="${domainInstance.id}"/></td>
    </tbody>
<table>

I have the following javascript snippet in my main gsp, that calls the template:

function selectAll(status) {

}

How do I select all checkboxes from the selectAll name?

edited Mar 12, 2012 at 17:44Dónal's user avatarDónal185k173 gold badges568 silver badges820 bronze badgesasked Mar 12, 2012 at 14:30user82302124's user avataruser823021241,1435 gold badges32 silver badges57 bronze badges

Add a comment       正确答案

3 Answers

68

Since you are using jQuery, you should use an onclick handler like below for selectAll.

$(':checkbox[name=selectAll]').click (function () {
  $(':checkbox[name=domainList]').prop('checked', this.checked);
});

Please note that the above code is going to look into the entire dom for the checkbox with name=selectAll and set the status of the checkbox with name=domainList.

Below is a slightly better version with minor markup change,

jsFiddle DEMO


$('#selectAllDomainList').click(function() {
  var checkedStatus = this.checked;
  $('#domainTable tbody tr').find('td:first :checkbox').each(function() {
    $(this).prop('checked', checkedStatus);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<table id="domainTable">
  <!-- Added ID -->
  <thead>
    <tr>
      <th>
        <!-- Added ID to below select box -->
        <input type="checkbox" name="selectAll" id="selectAllDomainList" />
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="checkbox" name="domainList" value="${domainInstance.id}" />
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="domainList" value="${domainInstance.id}" />
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="domainList" value="${domainInstance.id}" />
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="domainList" value="${domainInstance.id}" />
      </td>
    </tr>
  </tbody>
  <table>

Expand snippet


edited Oct 23, 2014 at 0:54answered Mar 12, 2012 at 14:34Selvakumar Arumugam's user avatarSelvakumar Arumugam79.2k15 gold badges120 silver badges134 bronze badges

  • This method is the way to go - unobtrusive JavaScript. Ryan, if you use this method, be sure to take the onclick attribute out of your selectAll input. – Surreal Dreams Mar 12, 2012 at 14:39

Add a comment7


$("input:checkbox").prop("checked", status);


edited Jul 19, 2012 at 12:42Luke's user avatarLuke11.4k43 gold badges60 silver badges69 bronze badgesanswered Mar 12, 2012 at 14:34Ray's user avatarRay21.5k5 gold badges48 silver badges64 bronze badgesAdd a comment2

to select all checkboxes with name = selectAll and set their status you can do


function
selectAll(status) {   $('input[name=selectAll]').each(function(){      $(this).prop('checked', status);   }); }


answered Mar 12, 2012 at 14:34Nicola Peluchetti's user avatarNicola Peluchetti76.1k31 gold badges142 silver badges191 bronze badgesAdd a comment

Your Answer

 来自   https://stackoverflow.com/questions/9669005/jquery-toggle-select-all-checkboxes/9669062#9669062   


普通分类: