简体   繁体   English

将id属性的值设置为html中的变量

[英]setting value of id attribute as variable in html

How to add id attribute of an element in HTML as a variable which is declared above in javascript. 如何在HTML中将元素的id属性添加为上面在javascript中声明的变量。

jQuery jQuery的

var table_row = $('table').find('tr#'+pid);  
var name = table_row.find('td:nth-child(1)').html();  
table_row.find('td:nth-child(6)').html('<button  type="button" id="what to write here" class ="save_db")>save</button> ');

I want to set id as name. 我想将id设置为名称。

Thanks 谢谢

It can be done using simple string concatenation as follows: 可以使用以下简单的字符串连接来完成:

var table_row = $('table').find('tr#'+pid);  
var name = table_row.find('td:nth-child(1)').html();  
table_row.find('td:nth-child(6)')
  .html('<button  type="button" id="'+name+'" class ="save_db">save</button> ');

PS: Also note that your markup contained a seemingly unnecessary closing brace after class attribute. PS:还请注意,您的标记在class属性之后包含了看似不必要的closing brace I've removed it in my code. 我已在代码中将其删除。

So you just want a variable inserted into the html string? 因此,您只想将变量插入html字符串中?

table_row.find('td:nth-child(6)').html`(
    '<button  type="button" id="' + name + '" class ="save_db">save</button> '
)`;

Concatenate name variable to the appending string as ID . name变量连接到附加字符串作为ID

table_row.find('td:nth-child(6)').html('<button  type="button" id="'+ name + '" 
class ="save_db")>save</button>');  

请遵循以下代码:

table_row.setAttribute("id", "id_you_like");

Please try with the below code snippet. 请尝试使用以下代码段。 Let me know if i am not understand your question. 如果我不明白您的问题,请告诉我。

var id = "btn1";
var table_row = $('table').find('tr#'+pid);  
var name = table_row.find('td:nth-child(1)').html();  
table_row.find('td:nth-child(6)').html('<button  type="button" id="'+id+'" class ="save_db")>save</button> ');

Have you tried this? 你有尝试过吗? Try using this: 尝试使用此:

<button type="button" id='"+name+"' class= "save_db">

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

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