简体   繁体   English

使用jquery获取表的特定行的值

[英]get value of a specific row of a table using jquery

How can I get a specific row value of a table(for exampe third row of fifth column) using jquery. 如何使用jquery获取表的特定行值(例如第五列的第三行)。 I tried: 我试过了:

var t = document.getElementById('table');

    var val1 =$(t.rows[2].cells[4]).val();
    alert(val1);

But it is showing nothing 但它什么也没有显示出来

Try using :eq() selector at this context 尝试在此上下文中使用:eq()选择器

.val() is only applicable for input elements, you should use .text() instead to retrieve its text content, .val()仅适用于输入元素,您应该使用.text()来检索其文本内容,

var val1 =$(t).find('tr:eq(2) td:eq(4)').text();
alert(val1);

Or do, 或者,做

alert($('#table tr:eq(2) td:eq(4)').text());

DEMO DEMO

.val is for textBox, textAreas, use .text instead like bellow. .val用于.val ,textAreas,使用.text而不是像bellow。

That should be something like bellow 那应该是像波纹管那样的东西

alert($('table tr').eq(2).find('td').eq(4).text())

I've added the selectors according to my DEMO, please change it accordingily, because I don't have your HTML 我根据我的DEMO添加了选择器,请根据我的要求进行更改,因为我没有您的HTML

DEMO DEMO

Assuming you want to access 5th column in 3rd row , 假设您要访问第3行中的第5列

You can use :eq() selector like 您可以使用:eq()选择器之类的

var value= $("#table tr:eq(2) td:eq(4)").text();

Side note: val() is used for accessing the value of form controls. 附注: val()用于访问表单控件的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM