Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在html中将id属性的值设置为变量_Javascript_Html - Fatal编程技术网

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

Javascript 在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_

如何在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> ');
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=“