【问题标题】:JQuery : select element inside hidden column of datatablesJQuery:选择数据表隐藏列内的元素
【发布时间】:2021-09-20 01:03:48
【问题描述】:

我有这样的代码:

<table class="table" id="items">
  <thead>
    <tr>            
      <th>Acc Name</th>
      <th class="hjob">Job</th>
    </tr>
  </thead>
  <tbody>
    <tr>                
      <td><input class="acname"></td>
      <td><input class="job"></td>
    </tr>
  </tbody>
</table>

在 dataTables 定义中,使用“hjob”类隐藏列标题:

$('#items').DataTable({ 
  columnDefs: [ { visible: false, targets: 'hjob' } ]
});

然后,我想选择 tr 标签内的所有元素(包括隐藏的元素)

$('tbody').find('tr:first').find('input').each(function() {
  console.log($(this).attr('class'));
});

但是,我只有“accname”(无法获得“job”)类。是否可以在隐藏列中选择元素/单元格?

提前致谢。

【问题讨论】:

  • 如果您使用的是datatables,那么您必须使用数据表 API 而不是 jquery 从数据中获取数据

标签: jquery hidden


【解决方案1】:

您的表格是否包含在table 元素中?添加它们使其如您所描述的那样工作 - 输出两个输入的类:

$('tbody').find('tr:first').find('input').each(function() {
  console.log($(this).attr('class'));
});
.hjob, .job {
visibility:hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>          
      <th>Acc Name</th>
      <th class="hjob">Job</th>
    </tr>
  </thead>
  <tbody>
    <tr>              
      <td><input class="acname"></td>
      <td><input class="job"></td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 感谢您的回答,但不幸的是,这段代码只是隐藏了列的标题,而不是整个列,包括我们隐藏电子表格列的内容。
  • 好的,代码被编辑了,但是不,这只是隐藏列的内容而不是列本身,表格列仍然在那里并占用屏幕空间。我使用dataTables,只是编辑我的问题更清楚。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
相关资源
最近更新 更多