【问题标题】:How to get the id of first <tr> of <tbody> in HTML table using jQuery?如何使用jQuery获取HTML表格中<tbody>的第一个<tr>的id?
【发布时间】:2014-05-05 05:46:50
【问题描述】:

我有以下 HTML 表格:

<table id="blacklistgrid_1"  class="table table-bordered table-hover table-striped">
                      <thead>
                        <tr id="jumbo">
                          <th style="vertical-align:middle">Products</th>
                          <th style="vertical-align:middle">Pack Of</th>
                          <th style="vertical-align:middle">Quantity</th>
                          <th style="vertical-align:middle">Volume</th>
                          <th style="vertical-align:middle">Unit</th>
                          <th style="vertical-align:middle">Rebate Amount</th>
                        </tr>
                      </thead>
                      <tbody class="apnd-test">
                        <tr id="reb1_1">
<td><input type="text" name="pack[1]" id="pack_1" value="" class="form-control" size="8"/></td>
                          <td><input type="text" name="quantity[1]" id="quantity_1" value="" class="form-control" size="8"/></td>
                          <td><input type="text" name="volume[1]" id="volume_1" value="" class="form-control" size="8"/></td>
<td><input type="text" name="amount[1]" id="amount_1" value="" class="form-control" size="9"/></td>
</tr>
                      </tbody>
<tfoot>
                        <tr id="reb1_2">
                          <td><button style="float:right; margin-bottom: 20px" class="products" type="button" class="btn btn-default" onclick="">&nbsp;Add</button></td>
                          <td></td>
                          <td></td>
                          <td></td>
                          <td></td>
                          <td></td>
                        </tr>
                      </tfoot>                                           
                    </table>

现在我想从 .在这种情况下,id 是reb1_1。为了得到它,我尝试了下面的代码,但没有成功。

$(document).ready(function() {
$('.products').click(function () {
var row = $(this).closest('table tbody:tr:first').attr('id');
    alert(row);
});

});

谢谢。

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

首先转到表 tbody 然后找到 tr 然后过滤第一行,如下所示

var row = $(this).closest('table').find(' tbody tr:first').attr('id');

或者可以试试

 var row = $(this).closest('table').find(' tbody tr:eq(0)').attr('id');

参考:first和:eq

【讨论】:

    【解决方案2】:

    如演示中所示进行更改:-

    http://jsfiddle.net/avmCX/38/

     $(document).ready(function() {
        $('.products').click(function () {
      var row = $(this).closest('table').find('tbody tr:first').attr('id');
    
    
            alert(row);
        });
    
        });
    

    更多信息请看这里:-

    http://api.jquery.com/first/

    【讨论】:

      【解决方案3】:

      把你的函数改成这样:

      $(document).ready(function() {
          $('.products').click(function () {
              var id =  $(this).closest("table").find("tbody>tr").first().attr("id");
              alert(id);
          });
      });
      

      这将向您显示 id。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-28
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 2017-10-15
        相关资源
        最近更新 更多