<html>
<head>
<title></title>
<script language="javascript">
function add() {
td = document.getElementById("tdNode");
var inText = document.createElement("input");
var button = document.createElement("input");
var br = document.createElement("br");
inText.type="text";
inText.name="test";
button.type="button";
button.value="del"
button.onclick=function del(){
td.removeChild(br);
td.removeChild(inText);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(inText);
td.appendChild(button);
}
</script>
</head>
<body>
<form action="">
<table border="1" width="35%">
<tr>
<td id="tdNode">
<input type = "text" name="test" >
<input type = "button" value = "submit" name="submit">
<input type = "button" value = "add" name="addMore" onclick ="add();">
</td>
</tr>
</table>
</form>
</body>
</html>
本文介绍了一种使用JavaScript动态向HTML页面中添加表单元素的方法。通过获取指定ID的表格单元格,创建并配置新的输入框及按钮,再将它们添加到目标单元格中。此外,还提供了一个删除已添加元素的功能。
1万+

被折叠的 条评论
为什么被折叠?



