使用 jquery 获取 tr 的第二个 td

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40214003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:14:33  来源:igfitidea点击:

Get second td of tr using jquery

jquery

提问by Syed

I'm trying to get td values of tr.. but unfortunately it's not working.. may be there is something wrong

我正在尝试获取 tr 的 td 值 .. 但不幸的是它不起作用.. 可能有什么问题

My html looks like

我的 html 看起来像

<tr class="dname">
     <td>abc</td>
      <td>value here</td>
</tr>

My jquery code is

我的 jquery 代码是

  jQuery(".dname").find("tr td:eq(1)").val();

What's wrong in this ?

这有什么问题?

回答by samjudson

jQuery find()method returns the descendants of the selected element. You are already selecting the <tr>with a class of dnameand then trying to find a descendant which is also a <tr>.

jQueryfind()方法返回所选元素的后代。您已经在选择<tr>类为 of 的dname,然后尝试查找也是<tr>.

https://api.jquery.com/find/

https://api.jquery.com/find/

The following should work:

以下应该工作:

jQuery(".dname").find("td:eq(1)").text();

Edit: text()instead of val()as @freedomn-m pointed out

编辑:text()而不是val()@freedomn-m 指出

回答by Tarun Khurana

Replace n with child no.

用孩子编号替换 n。

$(".dname td:nth-child(n)").text();

For e.g. for 2nd child

例如对于第二个孩子

$(".dname td:nth-child(2)").text();

回答by Darragh Enright

You're already filtering on the trby its class name, so there's no need to find by tragain.

您已经tr按其类名过滤了,因此无需tr再次查找。

jQuery(".dname").find("td:eq(1)").text()

Also, you need to .text()to get the contents of a <td>not .val().

此外,您需要.text()获取<td>not的内容.val()

JSBinhere.

JSB在这里。

Hope this helps :)

希望这可以帮助 :)

回答by SilentCoder

Because that <td>is the last child of your <tr>you can access it like below,

因为那<td>是你的最后一个孩子,<tr>你可以像下面那样访问它,

jQuery(".dname > td:last-child").text();

jQuery(".dname > td:last-child").text();