function moreFields() {
counter++;
var newFields = document.getElementById("input").cloneNode(true);
newFields.id = counter;
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].id;
if (theName) {
newField[i].id = theName + counter;
}
}
var insertHere = document.getElementById("buttons");
insertHere.parentNode.insertBefore(newFields,insertHere);
}
function clear() {
var count = counter;
while (count>0) {
var pricetxt = document.getElementById('price'+count);
pricetxt.value = "";
var qtytxt = document.getElementById('qty'+count);
qtytxt.value = "";
count--;
}
}
这是页面的jsfiddle
http://jsfiddle.net/xjsc16x/TctQx/1/
对不起,我知道它很大,但我不知道出了什么问题。moreFields 函数正确复制字段(只有 2 个文本框),但我不确定它是否正确地实现了递增的 id。
我认为我的 clear 功能正确,但它无法读取 ID?任何人都可以帮助我吗?