Javascript 在html中将id属性的值设置为变量
如何在HTML中添加元素的id属性作为上面在javascript中声明的变量 jQueryJavascript 在html中将id属性的值设置为变量,javascript,html,Javascript,Html,如何在HTML中添加元素的id属性作为上面在javascript中声明的变量 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_
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> ');
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('save');
我想将id设置为名称
谢谢所以您只想在html字符串中插入一个变量
table_row.find('td:nth-child(6)').html`(
'<button type="button" id="' + name + '" class ="save_db">save</button> '
)`;
table_row.find('td:nth child(6)').html`(
“保存”
)`;
将name
变量作为ID
连接到附加字符串中
table_row.find('td:nth-child(6)').html('<button type="button" id="'+ name + '"
class ="save_db")>save</button>');
table_row.find('td:nth child(6)').html('save');
请遵循以下代码:
table_row.setAttribute("id", "id_you_like");
请尝试使用下面的代码段。 如果我不明白你的问题,请告诉我
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> ');
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('save');
您试过这个吗?尝试使用以下方法:
<button type="button" id='"+name+"' class= "save_db">
可以使用以下简单的字符串连接来完成:
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> ');
var table_row=$('table').find('tr#'+pid);
var name=table_row.find('td:nth child(1)').html();
表_行查找('td:n第n个子项(6)')
.html('save');
另请注意,您的标记在
类
属性之后包含了一个看似不必要的右括号
。我已在代码中删除它。无法获取您的信息。请解释一下,我找不到你。请解释。(根据我的理解)他想使用javascript动态设置id。请检查他的代码片段中的“在这里写什么”文本。在id=“