用JS对HTML table动态增加行删除行问题?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用JS对HTML table动态增加行删除行问题?相关的知识,希望对你有一定的参考价值。

<html>
<base href="http://localhost:8080/web/planAndLog/userWorkLog/selectUserWorkLog.jsp">
<head>
<title></title>
<SCRIPT language="JavaScript">
var tempRow=0;
var maxRows=0;
function insertRows()
tempRow=table1.rows.length-1;
maxRows=tempRow;
tempRow=tempRow+1;
var Rows=table1.rows;//类似数组的Rows
var newRow=table1.insertRow(table1.rows.length);//插入新的一行
var Cells=newRow.cells;//类似数组的Cells
for (i=0;i<3;i++)//每行的7列数据

var newCell=Rows(newRow.rowIndex).insertCell(Cells.length);
newCell.align="center";
switch (i)

case 0 : newCell.innerHTML="<td valign='top'><input name='detailObject.seq' id='_Seq' type='text' size='2' maxlength='2' value='"+tempRow+"'></td>";break;
case 1 : newCell.innerHTML="<td valign='top'><select name='detailObject.xmxh' id='_Xmxh'><option value='' selected='selected'></option><option value='XMXH20060713150418343'>测试项目1</option><option value='XMXH20060725140312843'>能源部测试项目</option><option value='XMXH20060801143744796'>345fdhdfhfghgfjbc</option></select></TD>"; break;
case 2 : newCell.innerHTML="<td valign='top'><a href='javascript:delTableRow(\""+tempRow+"\")'>删除</a></TD>"; break;


maxRows+=1;

function delTableRow(rowNum)
if (table1.rows.length >rowNum)
table1.deleteRow(rowNum);


</SCRIPT>
</head>
<body>
<form action="">
<table border="0" cellspacing="0" cellpadding="0" width="98%" align="center">
<tr valign="top">
<th>
<input value="添加" type="button" onclick="insertRows()">
</th>
</tr>
</table>
<br />
<table border="1" width="98%" align="center" id="table1">
<tr>
<th >顺序号</th>
<th >项目序号</th>
<th>删除<th>
</tr>
</table>
</form>
</body>
</html>
用JS对HTML table动态增加行删除行问题

增加一行已经可以了。但删除总是不行。

从最后一行开始删除行,但从中间删除也可以,但再删除下面的就不行了。。

请高手帮助解决。

急!!!!!!

参考技术A 从中间删除时你要考虑你删除完这一行后会影响到你删除这行以下所有行的ID,以下的所有ID都会减1

以上是关于用JS对HTML table动态增加行删除行问题?的主要内容,如果未能解决你的问题,请参考以下文章

HTML table利用 JS动态增加行列 并且设置colspan

jquery 动态增加删除行

jquery 动态增加删除行

JQUERY方法给TABLE动态增加行

JQUERY方法给TABLE动态增加行

jQuery实现表格行的动态增加与删除(改进版)