首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Javascript Bootstrap表格单元格和列值

Javascript Bootstrap表格单元格和列值
EN

Stack Overflow用户
提问于 2018-08-03 04:38:38
回答 1查看 1.3K关注 0票数 0

访问我的表值时遇到问题。如何访问我单击的值的标题值。另外,我如何才能访问基于我单击的单元格的第一个索引?例如,单击姓名,但获得该人员姓名的Uid。

现在,这是检索单元格的值,但我需要更多的功能。

代码语言:javascript
复制
<script>

    $("#table").on("click", "td", function (row, $el, field) {

        var col = $(this)[0].textContent

        alert(col);

    });

</script>
EN

回答 1

Stack Overflow用户

发布于 2018-08-03 05:43:30

您需要使用.closest()从td向上遍历到其祖先tr,然后使用.find()和use.text()遍历该行的第一个td,以获得该单元格的文本内容。

通过将jquery方法链接在一起,这一切都可以一气呵成,但我在下面对其进行了分离,以显示步骤。

代码语言:javascript
复制
    $("#table").on("click", "td", function () {
        var parentTr = $(this).closest('tr');
        var firstCellContent  = parentTr.find('td:eq(0)').text();
       console.log(firstCellContent); // gives cell 1
    });
代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51661824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档