js实现动态添加、删除文本框。
例子:
代码示例:
动态添加、删除文本框 - www.var count = 0;
function Add(num){
count += 1;
var File1 = document.getElementById("file1");
var div = document.createElement("div");
var countTxt = document.createTextNode("文本框"+count);
var inputTxt = document.createElement("input");
inputTxt.type = "text";
inputTxt.value = document.getElementById(num).value;
inputTxt.name = "txt"+count;
var inputTxta = document.createElement("input");
inputTxta.type = "text";
inputTxta.value = document.getElementById("a"+num).value;
inputTxta.name = "txt"+count;
var inputTxtaa = document.createElement("input");
inputTxtaa.type = "text";
inputTxtaa.value = document.getElementById("aa"+num).value;
inputTxtaa.name = "txt"+count;
var inputTxtaaa = document.createElement("input");
inputTxtaaa.type = "text";
inputTxtaaa.value = document.getElementById("aaa"+num).value;
inputTxtaaa.name = "txt"+count;
var inputTxtaaaa = document.createElement("input");
inputTxtaaaa.type = "text";
inputTxtaaaa.value = document.getElementById("aaaa"+num).value;
inputTxtaaaa.name = "txt"+count;
var btn = document.createElement("input");
btn.type = "button";
btn.value = "删除";
btn.onclick = function()
{
this.parentNode.parentNode.removeChild(this.parentNode);
var n = File1.getElementsByTagName("div");
for(var k=0; k
{
n[k].firstChild.nodeValue = "文本框"+(k+1);
}
count -= 1;
}
div.appendChild(countTxt);
div.appendChild(inputTxt);
div.appendChild(inputTxta);
div.appendChild(inputTxtaa);
div.appendChild(inputTxtaaa);
div.appendChild(inputTxtaaaa);
div.appendChild(btn);
File1.appendChild(div);
}