2014-04-09 92 views
0

如何在HTML中将元素的id属性添加为以上在javascript中声明的变量。将id属性的值设置为html中的变量

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

无法找到你。请解释。 – Tuhin

+0

(根据我的理解)他想通过使用javascript varibale动态设置id。请在他的代码片段中查看“在这里写什么”文本。 –

回答

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 braceclass属性。我在代码中删除了它。

0

所以你只想要一个变量插入到HTML字符串?

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

name串联可变的附加的字符串作为ID

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

在'id =“'之前添加单引号 –

0

请点击此代码:

table_row.setAttribute("id", "id_you_like"); 
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> '); 
0

你试过吗?尝试使用这样的:

<button type="button" id='"+name+"' class= "save_db"> 
相关问题