簡體   English   中英

jQuery:獲取第四行(僅限)HTML表的第一列值

[英]jQuery: Get First Column Value on Fourth Row (only) of HTML Table

我有一個名為resultGridTable的表。 我有一個jQuery函數要在表的每一行上執行。 在函數中,“this”表示一行。

  1. 對於第四行,我需要提醒第一列值(第四行)。 我有以下代碼; 但它不起作用。 我們怎樣才能讓它發揮作用?

  2. 對於第五行,我需要提醒行中的列數。 我們怎么做?

  3. 在第六行的第二列中,我有兩個按鈕(輸入類型=“提交”)。 我需要提醒第二個按鈕的文本/值。 (第二個按鈕有一個名為“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);
    }
});

相關閱讀:

  1. jQuery代碼:從Parent到Child; 不是從孩子到父母

  2. 如何使用jQuery獲取html表中當前行中第一列的值

  3. 如何使用jQuery獲取Html表的第一行的最后一列

只是為了與眾不同,你可以在這里混合使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM