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

如何使用JQuery或Javascript更改行中特定TD的值?,javascript,jquery,html,Javascript,Jquery,Html,如果我有一张桌子: <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</t

如果我有一张桌子:

<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>

键1值1
键2值2
键3值3
是否有一种方法可以获取值2的“行索引”,并将值“Key2”存储到变量中。然后使用“包含值2的行的索引”,我将该行的“第一个TD”(或者如果有三个TD,则得到第三个TD)更改为类似“更改的键”的内容


类似于“tr指数”,然后指定“td指数”来更改。。。不确定jquery或javascript是否可以做到这一点

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

如果您知道行和单元格的索引,可以执行以下操作:

$(document).ready(function(){
    $('#someTable tr:nth-child(2) td:nth-child(1)').html('foo');
});
您也可以使用相同的选择器提取单元格的当前值,但是
.html()
而不是
.html(内容)

希望这有帮助

//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"