简体   繁体   English

如何通过ID jQuery从tr访问td

[英]How to access td from tr with id Jquery

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:

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

but it wipes all tds and it inserts the html in on column. 但它会擦除所有tds,并在on列中插入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. 但它也不起作用。

You need to give a space or a td before the :nth-child . 您需要在:nth-child之前加空格或td Space denotes a child or descendant selector: 空格表示子选择器或子代选择器:

$("#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! 选择<tr>的第二个实例,该实例不存在!

 $(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. 是的,当您使用#id进行选择时,不需要两个#id选择器。 Your first style works. 您的第一种风格有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM