简体   繁体   English

jQuery:获取第四行(仅限)HTML表的第一列值

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

I have a table named resultGridTable. 我有一个名为resultGridTable的表。 I have a jQuery function to be executed on each row of the table. 我有一个jQuery函数要在表的每一行上执行。 In the function, "this" means a row. 在函数中,“this”表示一行。

  1. 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? 我们怎样才能让它发挥作用?

  2. For the fifth row, I need to alert the number of columns in the row. 对于第五行,我需要提醒行中的列数。 How can we do it? 我们怎么做?

  3. 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? (第二个按钮有一个名为“secondButton”的类)我们怎么做呢?

Following is the code:: 以下是代码::

$('#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);
    }
});

READINGS: 相关阅读:

  1. jQuery Code: Starting from Parent to Child; jQuery代码:从Parent到Child; not from Child to Parent 不是从孩子到父母

  2. How to get value of first column in current row in html table using jQuery 如何使用jQuery获取html表中当前行中第一列的值

  3. How to get the first row's last column of an Html table using jQuery 如何使用jQuery获取Html表的第一行的最后一列

Just to be different, you can mix up DOM and jQuery to good effect here since you have fixed offsets into the table: 只是为了与众不同,你可以在这里混合使用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();

These should all be substantially faster than using selectors. 这些都应该比使用选择器快得多。

Look into jQuery eq : 看看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());

如果你有行/列的特殊值,考虑添加一个类,那么你可以使用选择器而不是可能改变的“魔术”数字。

Adapted from Accepted Answer. 改编自Accepted Answer。 This is the code if you are using jquery instead of document.getElementById The difference is that you need to insert the array of [0]. 如果您使用的是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