如何获得第一个的id <tr>& lt; tbody>在HTML表格中使用jQuery
How to get the id of first <tr> of <tbody> in HTML table using jQuery?
我有以下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=""> Add</button></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
现在我想获得第一行的id。本例中id为reb1_1
。为了得到它,我尝试了下面的代码,但它没有工作。
$(document).ready(function() {
$('.products').click(function () {
var row = $(this).closest('table tbody:tr:first').attr('id');
alert(row);
});
});
谢谢。
首先到table tbody,然后找到tr,然后像下面这样过滤第一行
var row = $(this).closest('table').find(' tbody tr:first').attr('id');
or can try
var row = $(this).closest('table').find(' tbody tr:eq(0)').attr('id');
<<p>引用strong>:首先和: eq 按演示所示进行更改:-
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/将函数改为:
$(document).ready(function() {
$('.products').click(function () {
var id = $(this).closest("table").find("tbody>tr").first().attr("id");
alert(id);
});
});
将显示id。
相关文章:
- 将HTML表格导出到excel时,无法将数据加载到excel文件中
- 打印预览没有应用程序页眉和页脚的html表格
- 将HTML表格导出到带有文本和图像的excel中
- JS图表和html表格重叠
- 带有单选按钮的html表格复制粘贴到xls中
- 应用程序脚本中导入HTML的数据擦除;谷歌表格
- HTML/JavaScript表格标题
- 如何使用谷歌脚本将html表单数据保存到谷歌电子表格中
- 如何在HTML中制作带导航的表格
- HTML表格导出为具有适当列宽和字体的pdf
- html表格的浮动水平滚动条
- 使用HTML / Javascript将Web应用程序生成的图表/表格转换和导出到可编辑的Powerpoint(.ppt
- 如何在不导致 HTML 表格失真的情况下正确使用追加
- 如何使用 JQuQuery 解析 JSON 对象并在 HTML 页面中以表格格式打印输出
- ModalDialog:HTML表格应该是可滚动的,而不是整个页面
- HTML表格在打印后缺少边框颜色和背景颜色
- 如何将表格html表格导出为PDF
- 从下拉列表中选择所选值后显示表格 (HTML)
- 导入Excel文件到表格HTML页面
- 用javascript将表格html转换为Excel