js插入一行(增加刪除行)

tracybb520 12年前發布 | 3K 次閱讀 MQTT
<html>
  <head>
    <title>添加表格信息</title>
 <meta http-equiv="pragma" content="no-cache" />
 <meta http-equiv="cache-control" content="no-cache" />
 <meta http-equiv="expires" content="0" />  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
 <meta http-equiv="description" content="This is my page" />
<script language="javascript" type="text/javascript">
function AddRow()
{
 //添加一行
 var i =tab1.rows.length;
 var Nam="'div1'";
 var Cod="fuJ"+i;
 var newTr = tab1.insertRow();
 //添加列
 var newTd0 = newTr.insertCell();
 var newTd1 = newTr.insertCell();
 var newTd2 = newTr.insertCell();
 var newTd3 = newTr.insertCell();
 //設置列內容和屬性
 newTd0.innerHTML = '<input type="checkbox" id="box1" onClick="GetRow()"/ /><input type="hidden" name="fuJ" id='+Cod+' />';
 newTd1.innerHTML = " 衣服尺碼";
 newTd2.innerHTML = " 衣服花色";
 newTd3.innerHTML = " 處理類型";
}
function DelRow()
{
//刪除一行
  var shu=0;
  var cheCou=document.all("box1");
   for(var i=0;i<cheCou.length;i++)
   {
    if (cheCou[i].checked==true)
    {
      shu++;
    }
   }
   if(shu==cheCou.length)
   {
     alert('最少要添加一件衣服');
     return;
   }
   else  if(shu==0)
   {
     alert('請選擇你要刪除的信息');
     return;
   }
   else if(shu==1)
   {
     for(var i=0;i<cheCou.length;i++)
     {
       if(cheCou[i].checked==true)
       {
        tab1.deleteRow(i+1);
       }
     }
   }
   else if(shu>1)
   {
     for(var a=0;a<shu;a++)
     {
         for(var i=0;i<cheCou.length;i++)
      {
        if(cheCou[i].checked==true)
        {
         tab1.deleteRow(i+1);
         break;
        }
      }
     }
   }
}
function GetRow()
{
  //獲得行索引
  //兩個parentElement分別是TD和TR喲,rowIndex是TR的屬性
  //this.parentElement.parentElement.rowIndex
  cGetRow=window.event.srcElement.parentElement.parentElement.rowIndex;
}
</script>
  </head>
  <body>
 <form name="form1" action="${pageContext.request.contextPath}/giveClothing.do?method=FuJiaFei" method="post"   >
 <center>
    <fieldset>
     <legend><strong>動態添加一行表格</strong></legend>
     <input type=hidden id="hid" name="hid"/ />
  <table id="tab1" align="center" border="1" cellPadding="4" cellSpacing="0" borderColorLight="#999999" borderColorDark="#ffffff" style="width:95%;word-break: break-all">
      <thead>
    <tr>
     <th align="center" width="10%">選擇</th>
     <th align="center" width="15%">衣服尺碼</th>
     <th align="center" width="15%">衣服花色</th>
     <th align="center" width="15%">處理類別</th>
    </tr>
   </thead>
               <tr >
     <td><input type=checkbox id="box1" onClick="GetRow()" />
         <input type="hidden" name="fuJ" id="fuJ1" />
     </td> 
     <td> 衣服尺碼</td>
     <td> 衣服花色</td>
     <td> 處理類別</td>
            </tr>

       </table>
       <table  align="center" border="1" cellPadding="4" cellSpacing="0" borderColorLight="#999999" borderColorDark="#ffffff" style="width:95%;word-break: break-all">
       <tr>
         <td align="right">
       <input type="button" name="Submit" value="增加一行" onclick="javascript:AddRow();" />
          <input type="button" name="Submit" value="刪除一行" onclick="javascript:DelRow();" />
         </td>
      </tr>
   </table>
</form>
  </body>

</html>
 本文由用戶 tracybb520 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!