[英]jQuery: Get First Column Value on Fourth Row (only) of HTML Table
我有一個名為resultGridTable的表。 我有一個jQuery函數要在表的每一行上執行。 在函數中,“this”表示一行。
對於第四行,我需要提醒第一列值(第四行)。 我有以下代碼; 但它不起作用。 我們怎樣才能讓它發揮作用?
對於第五行,我需要提醒行中的列數。 我們怎么做?
在第六行的第二列中,我有兩個按鈕(輸入類型=“提交”)。 我需要提醒第二個按鈕的文本/值。 (第二個按鈕有一個名為“secondButton”的類)我們怎么做呢?
以下是代碼::
$('#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);
}
});
相關閱讀:
只是為了與眾不同,你可以在這里混合使用DOM和jQuery,因為你已經在表中修復了偏移:
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();
這些都應該比使用選擇器快得多。
看看jQuery eq :
alert($('#resultGridTable tr:eq(3) > td:eq(0)').text());
alert($('#resultGridTable tr:eq(4) > td').length);
alert($('#resultGridTable tr:eq(5) > td:eq(1) > .secondButton').text());
如果你有行/列的特殊值,考慮添加一個類,那么你可以使用選擇器而不是可能改變的“魔術”數字。
改編自Accepted Answer。 如果您使用的是jquery而不是document.getElementById
,那么這就是代碼。區別在於您需要插入[0]數組。
var t = $('resultGridTable');
// jQuery to get the content of row 4, column 1
var val1 = $(t[0].rows[3].cells[0]).text();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.