jQuery:在第四行(仅)的HTML表中获取第一列值 [英] jQuery: Get First Column Value on Fourth Row (only) of HTML Table
问题描述
我有一个名为resultGridTable的表。我有一个jQuery函数要在表的每一行上执行。在函数中,this表示一行。
I have a table named resultGridTable. I have a jQuery function to be executed on each row of the table. In the function, "this" means a row.
-
对于第四行,我需要提醒第一列值(第四行)。我有以下代码;但它不工作。
For the fourth row, I need to alert the first column value (of fourth row). I have the following code; but it does not work. How can we make it working?
对于第五行,我需要提醒行中的列数。
For the fifth row, I need to alert the number of columns in the row. How can we do it?
在第六行的第二列中,我有两个按钮(input type =submit)。我需要提醒第二个按钮的文本/值。 (第二个按钮有一个名为secondButton的类)我们如何做呢?
In the sixth row's second column, I have two buttons (input type="submit"). I need to alert the second button's text/value. (The second button has a class named "secondButton") How can we do it?
:
$('#resultGridTable tr').each(function (i)
{
//"this" means row
//Other operations in the row. Common for all rows
if(i==3)
{
//Specific Operation for fourth row
var columnValue = $(this 'td:nth-child(0)').val();
alert(columnValue);
}
});
阅读:
how to get value of first column in current row in html table using jquery
推荐答案
只是为了不同,你可以混合使用DOM和jQuery在这里有很好的效果,因为你有固定的偏移到表中:
Just to be different, you can mix up DOM and jQuery to good effect here since you have fixed offsets into the table:
var t = document.getElementById('resultGridTable');
// jQuery to get the content of row 4, column 1
var val1 = $(t.rows[3].cells[0]).text();
// no jQuery here to get that row length!
var val2 = t.rows[4].cells.length;
// DOM access for the cell, and jQuery to find by class and get the text
var val3 = $('.secondButton', t.rows[5].cells[1]).text();
这些应该会显着快于使用选择器。
These should all be substantially faster than using selectors.
这篇关于jQuery:在第四行(仅)的HTML表中获取第一列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!