如何使用JQuery或Javascript在一行中更改特定TD的值?

时间:2022-08-14 20:16:37

So if I have table:

如果我有表格

<table id="someTable">
  <tr><td>Key1</td><td>Value1</td></tr>
  <tr><td>Key2</td><td>Value2</td></tr>
  <tr><td>Key3</td><td>Value3</td></tr>
</table>

Is there a way I can get the "index of the row" of value 2 and store the value "Key2" into a variable. Then using that "index of the row containing value2", I change the "first TD"(or if there were three TD's, get the 3rd TD) of that row to something like "changed key"?

是否有一种方法可以让我获得值2的“行索引”并将值“Key2”存储到一个变量中。然后使用包含value2的行索引,我将第一个TD(或者如果有三个TD,得到第三个TD)的行改为类似于“change key”?

Something like "index of tr" and then specifying "index of td" to change... not sure if that is possible with jquery or javascript.

比如“tr的索引”,然后指定“td的索引”来更改……不知道jquery或javascript是否可以做到这一点。

3 个解决方案

#1


8  

If you know the index of the row and cell, you could do something like this:

如果你知道行和单元格的索引,你可以这样做:

$(document).ready(function(){
    $('#someTable tr:nth-child(2) td:nth-child(1)').html('foo');
});

You can also pull the current value of the cell using the same selector, but .html() rather than .html(content)

您还可以使用相同的选择器来提取单元格的当前值,但是.html()而不是.html(内容)

Fiddle here

小提琴在这里

#2


1  

If you pass jQuery a td, you can get the index of it's containing row (relative to the other rows) with $(obj).parents("tr").index(); //obj is the td object passed in You can do the same to the table cell: $("#yourtable td").eq(index).text("Your Value"); //index is the index you wanna grab

如果您传递jQuery a td,您可以使用$(obj).parents(“tr”).index()获得包含行(相对于其他行)的索引;//obj是传入的td对象,您可以对表单元格执行相同的操作:$(“#yourtable td”).eq(索引)。文本(“价值”);//index是你想要抓取的索引

#3


0  

//First, obtain the td that contains 'Value2'
var $tdThatContainsValue2 = $("#someTable tr td").filter(function(){
    return $(this).html() == "Value 2";
});
//Then, obtain the first td in its row (it's first 'sibling');
$firstCellOfValue2row = $tdThatContainsValue2.siblings("td").eq(0);
alert($firstCellOfValue2row.html());  //Will show "Key 2"

Hope this helps

希望这有助于

#1


8  

If you know the index of the row and cell, you could do something like this:

如果你知道行和单元格的索引,你可以这样做:

$(document).ready(function(){
    $('#someTable tr:nth-child(2) td:nth-child(1)').html('foo');
});

You can also pull the current value of the cell using the same selector, but .html() rather than .html(content)

您还可以使用相同的选择器来提取单元格的当前值,但是.html()而不是.html(内容)

Fiddle here

小提琴在这里

#2


1  

If you pass jQuery a td, you can get the index of it's containing row (relative to the other rows) with $(obj).parents("tr").index(); //obj is the td object passed in You can do the same to the table cell: $("#yourtable td").eq(index).text("Your Value"); //index is the index you wanna grab

如果您传递jQuery a td,您可以使用$(obj).parents(“tr”).index()获得包含行(相对于其他行)的索引;//obj是传入的td对象,您可以对表单元格执行相同的操作:$(“#yourtable td”).eq(索引)。文本(“价值”);//index是你想要抓取的索引

#3


0  

//First, obtain the td that contains 'Value2'
var $tdThatContainsValue2 = $("#someTable tr td").filter(function(){
    return $(this).html() == "Value 2";
});
//Then, obtain the first td in its row (it's first 'sibling');
$firstCellOfValue2row = $tdThatContainsValue2.siblings("td").eq(0);
alert($firstCellOfValue2row.html());  //Will show "Key 2"

Hope this helps

希望这有助于