我是编程新手,我创建了一个表单。当我单击添加按钮时,我想在表格中插入这些元素
我已经知道如何添加标签和文本字段,我想知道如何在表格中添加行和插入新标签和文本字段
<SCRIPT type="text/javascript">
var x = 1;
function add(type) {
var newlabel = document.createElement("Label");
newlabel.for = "Name";
newlabel.innerHTML = "NAME: ";
var element = document.createElement("input");
element.setAttribute("type", type);
element.setAttribute("name", type);
element.setAttribute("value", "Name"+x);
element.setAttribute("id", "Name"+x);
element.setAttribute("style","width: 64");
var newlabel1 = document.createElement("Label");
newlabel1.for = "AGE";
newlabel1.innerHTML = "AGE: ";
var element1 = document.createElement("input");
element1.setAttribute("type", type);
element1.setAttribute("name", type);
element1.setAttribute("value", "AGE"+x);
element1.setAttribute("id", "AGE"+x);
element1.setAttribute("style","width: 64");
var foo = document.getElementById("fooBar");
foo.appendChild(newlabel);
foo.appendChild(element);
foo.appendChild(newlabel1);
foo.appendChild(element1);
x++;
}
</script>
<form id="fooBar" form name="form1" method="post" action="">
<table id = "mytable" table border="1">
<tr>
<th scope="col">name: </th>
<th scope="col">
<label for="name"></label>
<input type="text" name="name" id="name">
</th>
<th scope="col">age:</th>
<th scope="col">
<label for="name2"></label>
<input type="text" name="name2" id="name2">
</th>
</tr>
</table>
<INPUT type="button" value="Add" onClick="add('text')" style="width:100px;"/>
</form>