超級簡單Table表格添加,刪除行

Table表格添加,刪除行

最近需要表格添加刪除行,添加行簡單,刪除行就有點兒懵逼了,於是網上找實例,功夫不負有心人,終於讓我找到了,下面的是代碼,大家覺得可以,記得點贊哦!!!謝謝!
需要的拿去,轉載請在明顯的位置標註出出處!!!

<html>
<head>
	<title>table添加/刪除行</title>
</head>
<body>
<table id="testTbl" border=1>
	<tr>
		<td>性別</td>
		<td>姓名</td>
		<td>年齡</td>
	</tr>
	<tr>
		<td>
			<select name="a">
			   <option value="男">男</option>
			   <option value="女">女</option>
			</select>
		</td>
		<td>
			<input type="text" name="b">
		</td>
		<td>
			<input type="text" name="c">
		</td>
	</tr>
</table>
<input type="button" name="Submit2" value="添加" οnclick="addRow()">
<script>
function addRow(){

	//添加行
	var newTr = testTbl.insertRow();

	//添加列
	var newTd0 = newTr.insertCell();
	var newTd1 = newTr.insertCell();
	var newTd2 = newTr.insertCell();
	var newTd3 = newTr.insertCell();

	//設置列內容和屬性
	newTd0.innerText = document.all("a").options[document.all("a").selectedIndex].text;
	newTd1.innerText = document.all("b").value;
	newTd2.innerText = document.all("c").value;
	newTd3.innerHTML= '<input type="button" name="del" value="刪除" οnclick="del(this)">';
}
function del(o){
	//獲取表格
	var t=document.getElementById('testTbl');
	//刪除當前行
	t.deleteRow(o.parentNode.parentNode.rowIndex)
}
</script>
</body>
</html>

//動態添加行
function addRow(){
   var table = document.getElementById("tableID");
   var newRow = table.insertRow(); //創建新行
   var newCell1 = newRow.insertCell(); //創建新單元格
   newCell.innerHTML = ""; //單元格內的內容
   newCell.setAttribute("align","center"); //設置位置
}

//動態刪除行
function deleteRow(){
   var rowIndex = event.srcElement.parentElement.parentElement.rowIndex;
   var styles = document.getElementById("tableID");
   styles.deleteRow(rowIndex);
}

用克隆的方式

<html>
<head>
<script type="text/javascript" src="jquery-easyui/jquery.min.js"></script>
<script type="text/javascript">
var index = 0;
$(document).ready(function(){
  $("button").click(function(){
  index++;
    $("body").append($("p").clone().attr({'id':'name'+index,'name':'pwd'+index}));
  });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>複製每個 p 元素,然後追加到 body 元素</button>
</body>
</html>


下面是直接添加和刪除當前table表格,很好用

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<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" src="jquery-easyui/jquery.min.js"></script>
  </head>
  <body>
   <a href = "javascript:void(0);"  οnclick="a();">添加</a>
   <a href = "javascript:void(0);" οnclick="b();">顯示</a>
   <table id = "tab"></table>
  </body>
<script type="text/javascript">
	var index = 0;
	var a =  function(){
		index++;

		var tab = "<table id = 'tab_"+index+"' border = '1' >";
		tab += "<tr><td colspan = '6' align = 'center'>尺寸規格</td></tr>";
		tab += "<tr><td>長度</td><td><input οnblur='b("+index+");' id = 'cd_"+index+"' /></td><td align = 'right'>寬度</td><td><input οnblur='b("+index+");' id = 'kd_"+index+"' /></td><td>數量(個)</td><td><input  οnblur='c("+index+");' id = 'sl_"+index+"' /></td></tr>";
		tab += "<tr><td>擺放區域</td><td><input id = 'bfqy_"+index+"' /></td><td>單個面積(平方米)</td><td><input readonly = 'readonly' id = 'dgmj_"+index+"' /></td><td>總面積</td><td><input readonly = 'readonly' id = 'zmj_"+index+"' /></td></tr>";
		tab += "<tr><td>內容描述</td><td colspan = '4'><textarea rows='3' cols='70' id = 'content_"+index+"' ></textarea></td><td><input type = 'button' value = '刪除' onclick = 'del("+index+");' id = 'del_"+index+"' /></td></tr>";
		tab += "</table>";
		$('#tab').append(tab);
	}
	

	function del(ind){
		$('#tab_'+ind).remove();
	}

	function b(ind){
		var cdd = $('#cd_'+ind).val();
		var kdd = $('#kd_'+ind).val();
		if(cdd==''){cdd = 1;}
		if(kdd==''){kdd = 1;}
		if(cdd==''&&kdd==''){
			$('#dgmj_'+ind).val('');
		}else{
			$('#dgmj_'+ind).val(cdd * kdd);
		}
	}
	function c(ind){
		var cdd = $('#cd_'+ind).val();
		var kdd = $('#kd_'+ind).val();
		var sll = $('#sl_'+ind).val();
		if(cdd==''){cdd = 1;}
		if(kdd==''){kdd = 1;}
		if(sll==''){sll = 1;}
		if(cdd==''&&kdd==''&&sll==''){
			$('#zmj_'+ind).val('');
		}else if(cdd!=''&&kdd!=''&&sll!=''){
			$('#zmj_'+ind).val(cdd * kdd * sll);
		}else{
			$('#zmj_'+ind).val('');
		}
		
	}
</script>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章