访问我的表值时遇到问题。如何访问我单击的值的标题值。另外,我如何才能访问基于我单击的单元格的第一个索引?例如,单击姓名,但获得该人员姓名的Uid。
现在,这是检索单元格的值,但我需要更多的功能。
<script>
$("#table").on("click", "td", function (row, $el, field) {
var col = $(this)[0].textContent
alert(col);
});
</script>
发布于 2018-08-03 05:43:30
您需要使用.closest()从td向上遍历到其祖先tr,然后使用.find()和use.text()遍历该行的第一个td,以获得该单元格的文本内容。
通过将jquery方法链接在一起,这一切都可以一气呵成,但我在下面对其进行了分离,以显示步骤。
$("#table").on("click", "td", function () {
var parentTr = $(this).closest('tr');
var firstCellContent = parentTr.find('td:eq(0)').text();
console.log(firstCellContent); // gives cell 1
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table">
<tr>
<th> column 1<th>
<th> column 2<th>
<th> column 3<th>
<th> column 4<th>
</tr>
<tr>
<td> cell 1<td>
<td> cell 2<td>
<td> cell 3<td>
<td> cell 4<td>
</tr>
</table>
https://stackoverflow.com/questions/51661824
复制相似问题