【发布时间】:2017-12-05 17:22:54
【问题描述】:
我有一个要求,我需要在单击 html 表中的第一列时获取 html 表中的 2nd td 的值。我正在使用 jquery 来实现这一点。
$('.tbody').on('click','tr td:nth-child(1)', function () {
var id = $(this).closest("td").find('td:eq(1)').text();
alert(id)
});
我正在使用 onclick 函数来获取第二个 td 的值,如上面的代码所示。但是我得到一个空警报。我不知道我哪里出错了,请帮忙。 根据我的理解 $(this).closest("td").find('td:eq(1)').text() 应该搜索下一个 td 和 .text() 方法应该显示 td 内的值不是吗?
下面的sn-p会反映上面提到的问题
请帮忙!
提前致谢!
$(function () {
$('.tbody').on('click','tr td:nth-child(1)', function () {
var id = $(this).closest("td").find('td:eq(1)').text();
alert(id)
});
});
#items {
border-collapse: collapse;
width: 100%;
}
#items th {
background-color: #009999;
color: white;
border : 1px solid;
}
#items td{
border : 1px solid;
}
#items tbody{
height:200px;
overflow-y:auto;
}
#items thead,.tbody{
display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<table class="table table-striped table-bordered table-hover table-condensed" style="width: 300px; " id="items">
<thead>
<tr>
<th style="width:100px;">Fee Type</th>
<th style="width:100px;">Charges per Qty</th>
<th style="width:100px;">Qty</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td style="width:100px;">a</td>
<td style="width:100px;">b</td>
<td style="width:100px;">c</td>
</tr>
<tr>
<td style="width:100px;">d</td>
<td style="width:100px;">e</td>
<td style="width:100px;">f</td>
</tr>
</tbody>
</table>
</div>
【问题讨论】:
标签: javascript jquery html css