87,910
社区成员
发帖
与我相关
我的任务
分享
<!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>
<meta charset="utf-8">
<Style>
</style>
<script>
var count=0;//新加项计数
function addtr(){
var tb=document.getElementById("tb");//获取table对象
var tr=document.createElement("tr");//新建一行
var td=document.createElement("td");//新建一格
for(var i=0;i<3;i++){//3为一行中列数,可动态设定。
tr.appendChild(td.cloneNode(true));
}
var tdlist=tr.getElementsByTagName("td");
var tempinput;
for(var i=0;i<tdlist.length;i++){//循环为td中的输入框设置name属性,用于后台读取
tempinput=document.createElement("input");
tempinput.name="new"+count;
count++;
tempinput.type="text";
tempinput.style.width="100px";
tempinput.style.height="20px";
tdlist[i].appendChild(tempinput);
}
tb.appendChild(tr);//新加一行到表格中
}
</script>
</head>
<body >
<form action="" method="get" onsubmit="this.tdcount.value=count;">
<!-- 提交时把新增表格个数也加到后台。便于获取内容 -->
<table id="tb">
<tr>
<td>啊啊啊</td>
<td>哦哦哦</td>
<td>呵呵呵</td>
</tr>
</table>
<input type="submit" value="提交" />
<input type="hidden" value="" name="tdcount" />
</form>
<p onclick="addtr();" style="cursor:pointer;">添加一行</p>
<script type="text/javascript">
</script>
</body>
</html>