使用索引从整行获取单个值

时间:2017-11-24 14:24:54

标签: javascript jquery html-table

我使用下面的函数循环遍历表格中的所有td ..但我需要从行中获取单个单元格值

这是我的javascript函数:

$(document).ready(function () {
    $('#<%= gvView.ClientID %> input[type="checkbox"]').change(function () {
        $(this).closest('tr').children('td').each(function () {
            var signValue = $(this).html(); // here I am getting all td values but I need to get particular value
            alert(signValue);
        });
    });
});

如何在同一行中获取单个值?

<tr>
  <td>adata</td>
  <td>cert</td>
  <td>desc</td>
  <td>virt</td>
  <td>cess</td>
<tr>

我需要从上表中获得第四个td值

3 个答案:

答案 0 :(得分:2)

  

我需要从上表中获得第四个td值

使用eq

var forthValue = $(this).closest('tr').children('td').eq(3).text();
  

<强>索引

     

一个整数,表示元素的从0开始的位置。

因此,eq(3)将提供第4个 td

答案 1 :(得分:1)

根据0索引,使用$("tr td:eq(3)")作为选择器可让您选择 4th td &amp;然后使用$("tr td:eq(3)").text()你可以得到它的价值。

答案 2 :(得分:1)

从行开始:使用孩子的find(classSelector)eq(index)

$('#<%= gvView.ClientID %> input[type="checkbox"]').change(function () {
    var signValue =  $(this).closest('tr').children('td').eq(3).text();
    // or
    var signValue =  $(this).closest('tr').find('.cellClassName').text(); 

});