我们正在使用以下代码创建一个可以通过按按钮进行扩展的表单,但我们在向输入字段添加唯一名称时遇到问题。我们可以添加唯一 ID,但是当我们将其更改为名称时,它就停止工作了。帮助? :( 非常感谢您的建议。
var counter = 0
function cloneRow() {
counter ++;
var row = document.getElementById("rowToClone"); // find row to copy
var table = document.getElementById("tableToModify"); // find table to append to
var clone = row.cloneNode(true); // copy children too
var theName= row.name;
clone.name = theName + counter++; // change id or other attributes/contents
table.appendChild(clone); // add new row to end of table
}
最佳答案
试试这个:-
var clone = row.cloneNode(true); // copy children too
var theName= row.getAttribute('name');
clone.setAttribute('name', theName + counter++);// change id or other attributes/contents
关于javascript - 为克隆的表单元素添加唯一的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17044498/