获取所选行的最后一个td的文本

时间:2014-08-26 11:56:33

标签: jquery

我有这张桌子

<table id="search" class="order-table table ">
  <thead>
    <tr>
      <th>#</th>
      <th>Group Title</th>
      <th>Group Name</th>
    </tr>
  </thead>
  <tbody id="in_modal">
    <tr><td>4</td><td>armin van buuren</td><td>arminvanbuuren</td><tr>
    <tr><td>5</td><td>krep</td><td>eden</td></tr>
  </tbody>
</table>

我正在尝试使用

检索当前所选行的最后一个td的文本
var le_selected = $('#search tr td:last-child', $(this).parents('tr.selected')).text();
alert(le_selected);

但这让我感到空白。

如何获取最后一个td的文本?

3 个答案:

答案 0 :(得分:1)

以下是您的需求:

var le_selected = $(this).closest('tr.selected').find('td:last-child').text();

或者:

var le_selected = $('td:last-child', $(this).closest('tr.selected')).text();

.parents()旨在返回所有匹配的父母(可能不止一个)。您需要的是一种返回this .closest()的最近祖先(一个)的方法。

答案 1 :(得分:0)

尝试

var le_selected = $('td:last-child', $(this).parents('tr.selected')).text();

您的搜索将从选定的tr开始,然后深入查看其td后代,查找任何最后的子项(假设this是表).
What you have will look inside the selected row for a element with id
搜索中的某个节点,然后是其tr和td后代。

答案 2 :(得分:0)

$('tr.selected td:last-child').text()
相关问题