如何在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_db")>save</button> ');
我想将id设置为名称。
由于
答案 0 :(得分:1)
可以使用简单的字符串连接完成,如下所示:
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:另请注意,您的标记在closing brace
属性之后包含看似不必要的class
。我在我的代码中删除了它。
答案 1 :(得分:0)
所以你只想在html字符串中插入一个变量?
table_row.find('td:nth-child(6)').html`(
'<button type="button" id="' + name + '" class ="save_db">save</button> '
)`;
答案 2 :(得分:0)
将name
变量连接到追加字符串ID
。
table_row.find('td:nth-child(6)').html('<button type="button" id="'+ name + '"
class ="save_db")>save</button>');
答案 3 :(得分:0)
请遵循以下代码:
table_row.setAttribute("id", "id_you_like");
答案 4 :(得分:0)
请尝试使用以下代码段。 如果我不明白你的问题,请告诉我。
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> ');
答案 5 :(得分:0)
<button type="button" id='"+name+"' class= "save_db">