在JSP頁面用JS增加TABLE行及刪除行

<button onClick="add()">add</button>
<button onClick="del()">del</button>
<button onClick="test()">test</button>
<table id="t1" border="1"></table>
<script language="JavaScript">
var n=0;
function add(){
n++;
t1.insertRow().insertCell().innerHTML = '<input type="radio" name="tt"><input name="test'+n+'"><button οnclick="deleteRow(this)">delthis</button>';
//t1.insertRow().insertCell().innerHTML = '<input type="radio" name="tt"><input name="test'+t1.rows.length+'">';
}
function del(){
 var c = document.getElementsByName('tt');
 for(var i=0; i<c.length; i++)
 if(c[i].checked)
 t1.deleteRow(i);
}
function deleteRow(obj){
 alert(obj.parentElement.parentElement.parentElement.parentElement.id);
 alert(obj.parentElement.parentElement.rowIndex);
//t1.deleteRow(obj.parentElement.parentElement.rowIndex);
}
function test(){
for(i=0;i<t1.rows.length;i++) alert(t1.rows[i].cells[0].innerHTML);
}
</SCRIPT>

 

<html>
<body>
<form id="myForm">
<table id="table1">
 <tr id="tr1">
  <td id="td1">
<input id="myButton"
       type="button" value="Show my parent" οnclick="function1();">
      </td>
    </tr>
  </table>
</form>
<script language="JavaScript">
    function function1() {
        var m = document.all.myButton.parentElement.parentElement.parentElement.parentElement.id;
        alert("Parent element: <FORM>, ID = "+'"'+m+'"');
    }
</script>
cloneNode()方法克隆表格:
<input type=button value=clone name=ok οnclick=add()>
<table border=1>
<tr id=a1 name=a1>
<td><input></td><td><input></td>
</tr>
</table>
<script language=JavaScript>
i=1
function add(){
++i;
var newTR = a1.cloneNode(true);
newTR.id="a"+i;
newTR.name="a"+i;
a1.parentNode.insertAdjacentElement("beforeEnd",newTR);
//alert(a1.parentElement.innerHTML);
}
</script>

appendChild() & removeChild()方法:(firefox only)
<div id="oTest">1234 </div>
<button οnclick="oT=document.getElementById('oTest');oS=document.createElement('span');oS.textContent='1234 ';oT.appendChild(oS)">append +</button><button οnclick="oT=document.getElementById('oTest');oT.removeChild(oT.lastChild)">remove -</button>

2.

 

  <html>
<base href="[url]http://localhost:8080/web/planAndLog/userWorkLog/selectUserWorkLog.jsp[/url]">
<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=&#39;top&#39;><input name=&#39;detailObject.seq&#39; id=&#39;_Seq&#39; type=&#39;text&#39; size=&#39;2&#39; maxlength=&#39;2&#39; value=&#39;"+tempRow+"&#39;></td>";break;
case 1 : newCell.innerHTML="<td valign=&#39;top&#39;><select name=&#39;detailObject.xmxh&#39; id=&#39;_Xmxh&#39;><option value=&#39;&#39; selected=&#39;selected&#39;></option><option value=&#39;XMXH20060713150418343&#39;>测试项目1</option><option value=&#39;XMXH20060725140312843&#39;>能源部测试项目</option><option value=&#39;XMXH20060801143744796&#39;>345fdhdfhfghgfjbc</option></select></TD>"; break;
case 2 : newCell.innerHTML="<td valign=&#39;top&#39;><a href=&#39;javascript:delTableRow(/""+tempRow+"/")&#39;>删除</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" οnclick="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>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值