动态添加、删除文本框

动态添加、删除文本框,很实用!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
</head>
<body>
<script type="text/javascript">
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.length; 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);
}
</script>
<div id="file1"> </div>

<input name="xiyaoname" id="1" value="追加记录" type="button" onclick="Add(1);" />
<input name="hiddenField" type="hidden" id="a1" value="sex" />
<input name="hiddenField" type="hidden" id="aa1" value=" birthday" />
<input name="hiddenField" type="hidden" id="aaa1" value="shenggao" />
<input name="hiddenField" type="hidden" id="aaaa1" value="tizhong" />
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值