如何使用id Jquery从tr访问td

时间:2022-07-10 20:22:42

My tr has id and two columns(tds) I want to change the second td html dinamically by doing this:

我的tr有id和两列(tds)我想通过这样做改变第二个td html dinamically:

$("#value_"+ control_id+":nth-child(2)").html(type_id_name);

but it wipes all tds and it inserts the html in on column.

但它擦除了所有tds并将html插入到列中。

this is the table structure

这是表结构

<tr id="value_486">
  <td></td>
  <td></td>
<tr>

I tried different approaches as

我试过不同的方法

$("#tableID tr#value_"+ control_id+":nth-child(2)").html(type_id_name);

but it doesnt work either.

但它也不起作用。

1 个解决方案

#1


4  

You need to give a space or a td before the :nth-child. Space denotes a child or descendant selector:

你需要在:nth-​​child之前给出一个空格或一个td。 Space表示子项或子项选择器:

$("#tableID tr#value_"+ control_id+" :nth-child(2)").html(type_id_name);
$("#tableID tr#value_"+ control_id+" td:nth-child(2)").html(type_id_name);

Your current one without the space:

你现在的空间没有空间:

$("#tableID tr#value_"+ control_id+":nth-child(2)").html(type_id_name);

Selects the second instance of the <tr>, which is not there!

选择的第二个实例,它不存在!

$(function () {
  var control_id = 486;
  var type_id_name = "Praveen";
  $("#tableID tr#value_"+ control_id+" :nth-child(2)").html(type_id_name);
  $("#tableID tr#value_"+ control_id+" td:nth-child(2)").html(type_id_name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tableID">
  <tr id="value_486">
    <td></td>
    <td></td>
  <tr>
</table>

And yeah, you don't need two #id selectors, when you are using #id to select. Your first style works.

是的,当您使用#id选择时,您不需要两个#id选择器。你的第一个风格有效。

#1


4  

You need to give a space or a td before the :nth-child. Space denotes a child or descendant selector:

你需要在:nth-​​child之前给出一个空格或一个td。 Space表示子项或子项选择器:

$("#tableID tr#value_"+ control_id+" :nth-child(2)").html(type_id_name);
$("#tableID tr#value_"+ control_id+" td:nth-child(2)").html(type_id_name);

Your current one without the space:

你现在的空间没有空间:

$("#tableID tr#value_"+ control_id+":nth-child(2)").html(type_id_name);

Selects the second instance of the <tr>, which is not there!

选择的第二个实例,它不存在!

$(function () {
  var control_id = 486;
  var type_id_name = "Praveen";
  $("#tableID tr#value_"+ control_id+" :nth-child(2)").html(type_id_name);
  $("#tableID tr#value_"+ control_id+" td:nth-child(2)").html(type_id_name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tableID">
  <tr id="value_486">
    <td></td>
    <td></td>
  <tr>
</table>

And yeah, you don't need two #id selectors, when you are using #id to select. Your first style works.

是的,当您使用#id选择时,您不需要两个#id选择器。你的第一个风格有效。