javascript 实现动态增加行/删除行

运行效果:



 

HTML代码:

  <html>
  	<body>
  		<input type="button" value="new row" onclick="add_row()">
  		<table id="table2">
  		</table>
  	</body>
  </html>

javascript代码:

  <script>
 var row_index=0;    
 /*
  * 增加一行
  */
  function  add_row()   
  {   
    var table2 = document.getElementById("table2");
	row_index++;  
    var new_row=table2.insertRow(table2.rows.length);  
	new_row.setAttribute("id", "row"+row_index);   
	var new_col=new_row.insertCell(0);  
	new_col.innerHTML="<input type='file' name='filename"+row_index+"' size='87' >";  
    var new_col=new_row.insertCell(1);  
    new_col.innerHTML="&nbsp;<input type='button' value='删除' onclick='delete_row(this.parentNode.parentNode.rowIndex)'>";    
    document.getElementById("filename"+row_index).focus();	 
  }   
  /*
   * 删除一行
   */
  function delete_row(rowIndex)   
  {  
	var table2 = document.getElementById("table2");
    table2.deleteRow(rowIndex);  
  } 
  </script>

猜你喜欢

转载自huangqiqing123.iteye.com/blog/1477696