<html>
<head>
<title>动态添加表单元素
</title>
</head>
<script language="javascript">function AddElement(mytype){ var TemO=document.getElementById("add"); var newInput = document.createElement("input"); newInput.type=mytype; newInput.id="input1"; TemO.appendChild(newInput); var newline= document.createElement("br"); TemO.appendChild(newline); } function delElement(mytype){ var TemO=document.getElementById("add"); var newInput = document.getElementById("input1"); TemO.removeChild(newInput); var newline= document.getElementById("br"); TemO.removeChild(newline); } </script> <body> <input name="" type="button" value="新建文本框"> <input name="" type="button" value="新建复选框"> <input name="" type="button" value="新建单选框"> <input name="" type="button" value="新建文件域"> <input name="" type="button" value="新建密码框"> <input name="" type="button" value="新建提交按钮"> <input name="" type="button" value="新建恢复按钮"> <input name="" type="button" value="删除恢复按钮"> <br /> <form name="frm" method="get" action=""> <div id="add"> <input name="textfield" type="text"> <br /> </div> </form> </body>
</html>
[quote] </p> <pre><code>function AddElement(mytype){ var TemO=document.getElementById("add"); var newInput = document.createElement("input"); newInput.type=mytype; newInput.id="input1"; </code></pre> <p>[color=red]//在这里作者作用了input1作为Id,这样一来每添加一个元素,都是相同的Id,这样不太好,删除方法中查找元素时采用getElementById("input1"),不知道是要删除哪个元素[/color]</p> <pre><code> TemO.appendChild(newInput); var newline= document.createElement("br"); </code></pre> <p>[color=red]//在这里作者作创建了一个<br/>元素,但这里只是单纯创建它,是不符合上下文逻辑的,因为下面删除方法中查找<br/>元素时,是采用getElementById()的形式,应该为它加上Id[/color]</p> <pre><code> TemO.appendChild(newline); } function delElement(mytype){ var TemO=document.getElementById("add"); var newInput = document.getElementById("input1"); TemO.removeChild(newInput); var newline= document.getElementById("br"); TemO.removeChild(newline); } </script> [/quote] </code></pre> <p>将以上代码作如下修改后,你再试试看!!!</p> <p>[quote]<script language="javascript"><br><br> var elementCount = 0;<br><br> function AddElement(mytype){<br><br> var TemO=document.getElementById("add");<br><br> var newInput = document.createElement("input"); </p> <pre><code> elementCount = elementCount + 1; newInput.type=mytype; newInput.id="input"+(elementCount); TemO.appendChild(newInput); var newline= document.createElement("br"); newline.id = "br"+(elementCount); TemO.appendChild(newline); } function delElement(mytype){ var TemO=document.getElementById("add"); if (elementCount>0){ var newInput = document.getElementById("input"+elementCount); TemO.removeChild(newInput); var newline= document.getElementById("br"+(elementCount)); elementCount = elementCount - 1; TemO.removeChild(newline); } } </script> [/quote] </code></pre> <p>!!!!(注:是即时修改,所以不算是最优的解决方法,如果你想要,可以给我留言,我抽时间给你写一个。)</p>
不知道你对这些代码的意思了解不
[code="java"]
function AddElement(mytype){
var TemO=document.getElementById("add"); //获取add也就是你底下命名为add的div
var newInput = document.createElement("input");
//创建一个input标签
newInput.type=mytype; //input标签的类型为你传递上来的参数mytype
newInput.id="input1"; //input 标签的ID名为input1
TemO.appendChild(newInput); //在add的div里面增加一个子节点(也就是上面你创建的input标签)
var newline= document.createElement("br"); //创建一个换行符
TemO.appendChild(newline); //在add的div子节点上增加这个换行符号
}
function delElement(mytype){
var TemO=document.getElementById("add"); //同上
var newInput = document.getElementById("input1");
//获取id为input1的对象
TemO.removeChild(newInput);
//在temo中移除他
var newline= document.getElementById("br");
TemO.removeChild(newline); //移除换行
}
有些问题可能是你增加的名称全部为input1造成的,你可以试着改下
[/code]
你可以拷贝到dreamweaver运行一下看是不是你想要的答案。动态的添加,删除table行
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1 | 2 | 3 |
veryd good