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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10796559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:57:18  来源:igfitidea点击:

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

javascriptjqueryhtmlcss

提问by LCJ

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.

我有一个名为 resultGridTable 的表。我有一个要在表的每一行上执行的 jQuery 函数。在函数中,“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?

  1. 对于第四行,我需要提醒第一列值(第四行)。我有以下代码;但它不起作用。我们怎样才能让它发挥作用?

  2. 对于第五行,我需要提醒行中的列数。我们怎么做?

  3. 在第六行的第二列,我有两个按钮(输入类型=“提交”)。我需要提醒第二个按钮的文本/值。(第二个按钮有一个名为“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; not from Child to Parent

  2. How to get value of first column in current row in html table using jQuery

  3. How to get the first row's last column of an Html table using jQuery

  1. jQuery 代码:从父到子开始;不是从孩子到父母

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

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

回答by Alnitak

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 shouldall be substantially faster than using selectors.

这些都应该比使用选择器快得多。

回答by Linus Gustav Larsson Thiel

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());

回答by Jeff Watkins

If you have special values for rows/columns, consider adding a class to it, then you can use selectors rather than "magic" numbers which could change.

如果您对行/列有特殊值,请考虑向其添加一个类,然后您可以使用选择器而不是可以更改的“魔术”数字。

回答by cweitat

Adapted from Accepted Answer. This is the code if you are using jquery instead of document.getElementByIdThe difference is that you need to insert the array of [0].

改编自接受的答案。这是代码,如果你使用jquery而不是使用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();