【问题标题】:Select all checkbox in the same column选中同一列中的所有复选框
【发布时间】:2016-09-16 20:42:21
【问题描述】:

我正在尝试实现类似于Checking checkbox in column

我在一个表中有两个选中所有复选框,选择一个将选中同一列的所有复选框。

它是一个 ASP.NET GridView。 Plunker

function CheckHeaderCheckboxAll(obj, gridname) {    
    var objId = obj.id;
    var table= $(obj).closest('table');
    $('td input:checkbox',table).prop('checked',this.checked);
}

有人可以帮我解决这个问题吗?

【问题讨论】:

  • obj是勾选的复选框吗?
  • this 此处不提及任何内容
  • 是的@epascarello。 obj 是选中的复选框

标签: javascript jquery gridview checkbox


【解决方案1】:

基本思想是选择单元格,获取索引,然后选择具有该索引的表格的其他表格单元格。

$("th input[type='checkbox']").on("change", function() {
   var cb = $(this),          //checkbox that was changed
       th = cb.parent(),      //get parent th
       col = th.index() + 1;  //get column index. note nth-child starts at 1, not zero
   $("tbody td:nth-child(" + col + ") input").prop("checked", this.checked);  //select the inputs and [un]check it
});
th { background-color: #CCC; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>
        <input type="checkbox">
      </th>
      <th>
        <input type="checkbox">
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="checkbox">
      </td>
      <td>
        <input type="checkbox">
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox">
      </td>
      <td>
        <input type="checkbox">
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox">
      </td>
      <td>
        <input type="checkbox">
      </td>
    </tr>
  </tbody>
</table>

如果您想像拥有的那样使用内联事件处理程序来执行此操作,您的代码将如下所示

function CheckHeaderCheckboxAll(obj) {
   var cb = $(obj),           //checkbox that was changed
       th = cb.parent(),      //get parent th
       col = th.index() + 1;  //get column index. note nth-child starts at 1, not zero
   $("tbody td:nth-child(" + col + ") input").prop("checked", obj.checked);  //select the inputs and [un]check it
}

【讨论】:

  • 谢谢。我试图从这个答案how-to-check-and-uncheck-all-checkboxes$("tbody td:nth-child(" + col + ") input").prop("checked", obj.checked);,但遇到语法错误。我现在明白了。
【解决方案2】:

移除点击事件。而是将它与类绑定。

<input type="checkbox" value="" class="checkAll" data-class="class1" name="checkAll" id="ctl00_UserMasterContentPlaceHolder_grdUserDetails_ctl01_chkHeaderCheckbox"/>

给出你要检查的类名

<input type="checkbox" value="" class="class1" />

试试这个jquery:

$(".checkAll").click(function () {
     var datacls = $(this).attr('data-class');
     $('input:checkbox.'+datacls).not(this).prop('checked', this.checked);
});

【讨论】:

    【解决方案3】:

    嗯,@epascarello 说的可能更好。

    我只有一个小建议,那就是从同一个表本身中查找复选框,否则最终会选择页面所有表中存在的复选框:

    function SelectAll(obj) {
    
      // find the index of column
      var table = $(obj).closest('table'); // find the current table
      var th_s = table.find('th'); // find/get all the <th> of current table
      var current_th = $(obj).closest('th'); // get current <th>
    
      // the value of n is "1-indexed", meaning that the counting starts at 1
      var columnIndex = th_s.index(current_th) + 1; // find index of current <th> within list of <th>'s
    
      console.log('The Column is = ' + columnIndex); // print the index for testing
    
      // select all checkboxes from the same column index of the same table
      table.find('td:nth-child(' + (columnIndex) + ') input').prop("checked", obj.checked);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
      <tr>
        <th>
          <input type="checkbox" onclick="SelectAll(this)" />
        </th>
        <th>Name</th>
        <th>
          <input type="checkbox" onclick="SelectAll(this)" />Is Active</th>
      </tr>
      <tr>
        <td>
          <input type="checkbox" />
        </td>
        <td>John</td>
        <td>
          <input type="checkbox" name="isActive" />
        </td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" />
        </td>
        <td>Tim</td>
        <td>
          <input type="checkbox" name="isActive" />
        </td>
      </tr>
    </table>
    <br/>
    <h2>
    Another Table<hr/>
    </h2>
    <table>
      <tr>
        <th>
          <input type="checkbox" onclick="SelectAll(this)" />
        </th>
        <th>Name</th>
        <th>
          <input type="checkbox" onclick="SelectAll(this)" />Is Active</th>
      </tr>
      <tr>
        <td>
          <input type="checkbox" />
        </td>
        <td>John</td>
        <td>
          <input type="checkbox" name="isActive" />
        </td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" />
        </td>
        <td>Tim</td>
        <td>
          <input type="checkbox" name="isActive" />
        </td>
      </tr>
    </table>

    供参考:

    nth-child 选择其父元素的第 n 个子元素。

    Fiddle Here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 2014-11-06
      • 1970-01-01
      • 2019-11-24
      相关资源
      最近更新 更多