【问题标题】:Jquery - not selecting td based on th classJquery - 不根据类选择 td
【发布时间】:2015-12-22 16:37:41
【问题描述】:

当我们点击一​​行时,我使用以下 jQuery 在第一列中选​​择一个复选框,除非我们点击一​​行中的最后一个单元格。

tableid.on('click', 'tbody td:not(td:last-child)', function (e) {
    $(this).parent().find('input[type="checkbox"]').trigger('click');
}); 

我将noselecting 类添加到thead>th。现在,就像我使用 :not(td:last-child') 跳过最后一个单元格一样,我如何才能从点击列上的第 td>th 类 noselecting 中跳过 td?

【问题讨论】:

  • 我认为你正在尝试做this?
  • 我正在尝试选择一个 tbody>td 如果存在 thead>th.noselecting。

标签: jquery


【解决方案1】:

您可以使用索引通过点击处理程序对其进行测试

var tableid = $('#mytable');
tableid.on('click', 'tbody td:not(td:last-child)', function(e) {
  var $this = $(this),
    index = $this.index();
  if (!$this.closest('table').find('th').eq(index).hasClass('noselecting')) {
    $this.parent().find('input[type="checkbox"]').trigger('click');
  }
});
td,
th {
  border: 1px solid grey;
}
.noselecting {
  background-color: lightblue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mytable">
  <thead>
    <th>1</th>
    <th class="noselecting">2</th>
    <th>3</th>
    <th>4</th>
    <th class="noselecting">5</th>
  </thead>
  <tbody>
    <td>1</td>
    <td>
      <input type="checkbox" />
    </td>
    <td>1</td>
    <td>1</td>
    <td>1</td>
  </tbody>
</table>

【讨论】:

    猜你喜欢
    • 2012-07-29
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    相关资源
    最近更新 更多