動態添加表格一行(javascript)

來源:互聯網
上載者:User

 <%&#64; page language="java" import="java.util.*,com.page.wash.giveclothing.*" pageEncoding="gbk"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<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 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> 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.