2010-12-05 27 views
2

我想用按钮附加一些元素。但他们必须有不同的ID。 我必须使用数组吗?像checkboxname [+ i]一样。 我使用javascript附加复选框列表需要数组吗?

在此先感谢

function append() 
    { 
     var cb = document.createElement("input"); 
     cb.type = "checkbox"; 
     cb.id = "id" 
     cb.checked = false; 

     var textfield = document.createElement("input"); 
     var delbtn = document.createElement("input"); 
     delbtn.type = "button"; 
     delbtn.value = "remove"; 
     delbtn.onclick= function(){remove()} 


     document.getElementById('append').appendChild(cb); 
     document.getElementById('append').appendChild(textfield); 
     document.getElementById('append').appendChild(delbtn); 

回答

3

您可以使用一个计数器来跟踪的ID的

鉴于你的代码中,我重构,并使用原生JS neated了一点& jQuery。

例如:(使用本地JS):

http://jsfiddle.net/4Y8mb/34/

例如:(使用jQuery):

http://jsfiddle.net/4Y8mb/18/

你不是很具体。你可以使用for循环,但如果你希望它发生在点击按钮上for循环不适用