表格动态追加、插入、删除行

<HTML>
<HEAD>
<TITLE>表格动态追加、插入、删除行</TITLE>
</HEAD>
<BODY >
<table id="DetailTable" border="1" width="60%" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" bordercolordark="#ffffffff"  >
 <tr>
    <td width="40%" align="center" bgcolor="#999999">姓名</td>
    <td width="20%" align="center" bgcolor="#999999">性别</td>
    <td width="40%" align="center" bgcolor="#999999">年龄</td>
 </tr>
    <tr>
    <td width="40%" align="center">&nbsp;&nbsp;<input type="text" name="username" style="width:200px"/></td>
    <td width="20%" align="center">
      <select style="width:100px">
          <option value="boy">男</option>
          <option value="girl">女</option>
      </select>
    </td>
    <td width="40%" align="center"><input type="text" name="age" style="width:200px"/></td>
 </tr>
</table>
<table id="TmpTable" style="display:none;">
    <tr>
    <td width="40%" align="center"><input name="msgid" type="radio" value="msgid"><input type="text" name="username" style="width:200px"/></td>
    <td width="20%" align="center">
      <select style="width:100px">
          <option value="boy">男</option>
          <option value="girl">女</option>
      </select>
    </td>
    <td width="40%" align="center"><input type="text" name="age" style="width:200px"/></td>
 </tr>
</table>
<br>
<table border="0" width="96%" align="center" >
 <tr><td align="center">
  <input type=button class="appButton" onClick="doAppend(DetailTable)" value="&nbsp;追&nbsp;加&nbsp;" >
  <input type=button class="appButton" onClick="doInsert(DetailTable)" value="&nbsp;插&nbsp;入&nbsp;" >
  <input type=button class="appButton" onClick="doRemove(DetailTable)" value="&nbsp;删&nbsp;除&nbsp;" >
 </td>
</table>

<script language="javascript">
 function doAppend(obj){
  if(obj==null){
   return;
  }
  rows=obj.rows;
  if(rows==null){
   return;
  }
  srcElem=rows[rows.length-1];
  var tmpTable=TmpTable;
  if(tmpTable==null){
   return;
  }
  var trows=tmpTable.rows;
   if(trows==null){
    return;
   }   
   newrow = obj.insertRow(srcElem.rowIndex+1);
   temprow=trows[0].cloneNode(true);
   newrow.replaceNode(temprow);
  newrow=null;
  tmprow=null;
 }
 function removeAllRow(obj){
  if(obj==null){
   return;
  }
  rows=obj.rows;
  for(i=1;i<rows.length;i++){
   desc=rows[i];
   obj.deleteRow(desc.sectionRowIndex);
    i--  
   rows=obj.rows;
  }
 }
 function doInsert(obj){
  var srcElem=getSelectedRow(obj);
  if(srcElem==null){
   alert("请选择要插入的行");
   return;
  }
  var tmpTable=TmpTable;
  if(tmpTable==null){
   return;
  }
  var trows=tmpTable.rows;
   if(trows==null){
    return;
   }  
   window.status="";
   newrow = obj.insertRow(srcElem.rowIndex);
   temprow=trows(0).cloneNode(true);
   newrow.replaceNode(temprow);
   window.status="";
  
   }
 function doRemove(obj){  
  srcElem=getSelectedRow(obj);
  if(srcElem==null){
   alert("请选则要删除的行");
   return;
  }    
  obj.deleteRow(srcElem.sectionRowIndex);
 }
 function getSelectedRow(obj){
  if(obj==null){
   return null;
  }
  rows=obj.rows;
  if(rows==null){
   return null;
  }
  var field=document.all.msgid;
  if(field==null){
   return null;
  }
  var srcElem=null;
  for(i=0; i<field.length; i++){
   if(field[i].checked){
    srcElem=field[i];
    break;
   }
  }
  if(srcElem==null){
   return null;
  }
  while (srcElem.tagName != "TR"){
   srcElem = srcElem.parentElement;
     }  
  return srcElem;
 }
</script>
</BODY>
</HTML>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值