简体   繁体   English

在行上单击表中获取隐藏的列值

[英]Get the hidden column value in a table on row click

I have a table with four columns. 我有一张有四列的桌子。 The first two columns are hidden. 前两列是隐藏的。 I want to get the values of the second and third columns on row click. 我想获得第二行和第三列的值。 I can get the value of the third column using the below code. 我可以使用下面的代码获取第三列的值。 But how can I get the value of the hidden column? 但是,如何获取隐藏列的值?

$('body').on( 'click','#item-grid table tbody tr', function() {
    $('#PurchaseOrder_supplier_name').val($(this).children(':nth-child(3)').text());
});

Below is the table html. 下表是html表。

<table class="table table-bordered table-condensed table-hover table-striped dataTable">
  <thead>
    <tr>
      <th id="item-grid_c0" style="display:none">Supplier ID</th>
      <th id="item-grid_c1" style="display:none">Supplier ID</th>
      <th id="item-grid_c2"><a href="/builders_common/index.php?r=purchase/purchase/multipurchaseorderdetailview&amp;PurchaseOrder%5Bvoucher_no%5D=12&amp;PurchaseOrderDetails%5Bpurchase_voucher_no%5D=12&amp;PurchaseOrderDetails%5Bproject_id%5D=45&amp;PurchaseOrderDetails%5Bitem_id%5D=79&amp;ajax=item-grid&amp;sort=supplier"
        class="sort-link">Supplier</a>
      </th>
      <th id="item-grid_c3"><a href="/builders_common/index.php?r=purchase/purchase/multipurchaseorderdetailview&amp;PurchaseOrder%5Bvoucher_no%5D=12&amp;PurchaseOrderDetails%5Bpurchase_voucher_no%5D=12&amp;PurchaseOrderDetails%5Bproject_id%5D=45&amp;PurchaseOrderDetails%5Bitem_id%5D=79&amp;ajax=item-grid&amp;sort=item"
        class="sort-link">Item</a>
      </th>
      <th id="item-grid_c4"><a href="/builders_common/index.php?r=purchase/purchase/multipurchaseorderdetailview&amp;PurchaseOrder%5Bvoucher_no%5D=12&amp;PurchaseOrderDetails%5Bpurchase_voucher_no%5D=12&amp;PurchaseOrderDetails%5Bproject_id%5D=45&amp;PurchaseOrderDetails%5Bitem_id%5D=79&amp;ajax=item-grid&amp;sort=rate"
        class="sort-link">Rate</a>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr class="odd selected">
      <td style="display:none">
        <input type="hidden" id="ProjectPurchaseOrderSupplierwise_item_id_5" name="ProjectPurchaseOrderSupplierwise[item_id_5]" value="79" class="gridfield">
      </td>
      <td style="display:none">
        <input type="hidden" id="ProjectPurchaseOrderSupplierwise_supplier_id_5" name="ProjectPurchaseOrderSupplierwise[supplier_id_5]" value="14" class="gridfield">
      </td>
      <td>General</td>
      <td>Cement</td>
      <td>
        <input type="text" id="ProjectPurchaseOrderSupplierwise_rate_5" name="ProjectPurchaseOrderSupplierwise[rate_5]" value="50.00" readonly="readonly" class="gridfield">
      </td>
    </tr>
  </tbody>
</table>

Try like below. 尝试如下。

$('body').on( 'click','#item-grid table tbody tr', function() {
    $(this).find('td:eq(1) input').val(); // 2nd column
    $(this).find('td:eq(2)').text(); // 3rd column
});

You can use either of the two :eq() Selector or :nth-child() Selector. 您可以使用两个:eq()选择器或:nth-​​child()选择器中的任何一个。

https://api.jquery.com/nth-child-selector/ https://api.jquery.com/nth-child-selector/

https://api.jquery.com/eq-selector/ https://api.jquery.com/eq-selector/

Click here Example code with your HTML 单击此处HTML附带的示例代码

To get a specific cell by index, you can use : 要通过索引获取特定的单元格,可以使用:

 $(this).find('td:nth-child(2) input').val(); // 2nd column

 $(this).find('td:eq(1) input').val(); // 2nd column

 $(this).find('td:nth-child(3)').text(); // 3rd column
 $(this).find('td:eq(2)').text(); // 3rd column

\n
\n
\n
$('#PurchaseOrder_supplier_name').val($(this).children(':nth-child(3):visible').text());
\n
\n
\n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM