表格插入行与删除行示例

<html>
 <head>
  <title>表格插入行与删除行示例</title>
  
  <script type="text/javascript">
   //参数列表[表格Id,复制行的索引,标题行数,表单Id]
   function clickInsert(inputTable,rowId,titleRow,inputForm){
    var oForm=document.getElementById(inputForm);
    var oTable=document.getElementById(inputTable);
    var obj =window.event.srcElement;
    var oTd = obj.parentElement;
    var oTr = oTd.parentElement;
    var oRow = oTr.rowIndex;
    var oTr1 = oTable.insertRow(oRow+1);
    var oCells = oTr.cells;
    for(i=0;i<oCells.length;i++){
     var cell = oCells[i].innerHTML;
     var cell1 = oTr1.insertCell(i);
     cell1.innerHTML=cell;
    }
    oForm.flag[oRow+1-titleRow].value="1";
    //alert(oForm.flag[oRow+1-titleRow].value="1");
   }
   
   
   //参数列表[表格Id,标题行数,表单Id]
   function clickDelete(inputTable,titleRow,inputForm){
    var oForm=document.getElementById(inputForm);
    var oTable=document.getElementById(inputTable);
    var obj =window.event.srcElement;
    var oTd = obj.parentElement;
    var oTr = oTd.parentElement;
    var oRow = oTr.rowIndex;
                oTr.style.display="none";
    oForm.flag[oRow-titleRow].value="2";
    // alert(oForm.flag[oRow-titleRow].value);
   }
 
   function submitForm(){
    var flags= inputForm.flag;
    for(i=0;i<flags.length;i++){
     alert(flags[i].value);
    }
   }
  </script>
 </head>
 <body>
  <form action="" name="inputForm" id="inputForm">
   <table id="inputTable" width="50%" align="center" border="1" cellpadding="0"
    cellspacing="0">
    <tr>
     <td>
      学生
     </td>
     <td>
      课程
     </td>
     <td>
      &nbsp;
     </td>
     <td>
      &nbsp;
     </td>
    </tr>
    <tr style="display: none;">
     <td>
      <input type="hidden" name="flag" id="flag" value="-1" />
      <select id="studentId">
       <option value="01">
        张三
       </option>
       <option value="02">
        李四
       </option>
      </select>
     </td>
     <td>
      <select id="courseId">
       <option value="01">
        英语
       </option>
       <option value="02">
        数学
       </option>
      </select>
     </td>
     <td>
      <input type="button" name="insertButton" value="新增" />
     </td>
     <td>
      <input type="button" name="deleteButton" value="删除" />
     </td>
    </tr>
    <tr>
     <td>
      <input type="hidden" name="flag" id="flag" value="1" />
      <select id="studentId">
       <option value="01">
        张三
       </option>
       <option value="02">
        李四
       </option>
      </select>
     </td>
     <td>
      <select id="courseId">
       <option value="01">
        英语
       </option>
       <option value="02">
        数学
       </option>
      </select>
     </td>
     <td>
      <input type="button" name="insertButton" value="新增"
       οnclick="clickInsert('inputTable',1,1,'inputForm')" />
     </td>
     <td>
      <input type="button" name="deleteButton" value="删除"
       οnclick="clickDelete('inputTable',1,'inputForm')" />
     </td>
    </tr>
   </table>
    <br />
    <center><input type="button" name="submitButton" value="提交" οnclick="submitForm()" /></center>
  </form>
 </body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值