jquery动态添加或删除表(table)的行(tr)

功能描述:点击表格某一行之后的按钮实现对该行的添加删除操作(实现后为下图)


代码:

<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<html>
<head>
<title>add tr demo</title>
<style type="text/css">
	.datatable td {
	border-bottom: 1px solid #e4e5e5;
	height: 35px;
	line-height: 35px;
	text-align: left;
	padding-left: 10px;
	}
</style>
<script type="text/javascript"	src="/AddTrDemo/js/jquery-1.9.1.js"></script>
<script type="text/javascript" charset="UTF-8">

	function addTr(obj){
	     var tr = $(obj).parent().parent().clone();  //当前obj的父节点td的父节点tr
	     tr.appendTo("#fatable");
/* 	     $("#fatable tr:last input[type=text]").val(0);    默认值设置为0*/

	}
	function delTr(obj){
		var trlen = $("#fatable tr").length;
		if(trlen==1){
			showError('Cannot Be Deleted');
			return false;
		}
		$(obj).parent().parent().remove(); 
	}
</script>

</head>
<body style="text-align:center">
	<form id="" method="post" action="">
		<div class="pop-body">
			<div style="padding:0 20px; margin-top:10px;margin-bottom: 20px;">
                 <div style="position:relative; z-index:1000px;">
                   <table width="100%">
                     <tbody><tr>
                       <th width="22%">Plan </th>
                       <th width="2%">|</th>
                       <th width="22%">Quantity (packs)</th>
                       <th width="2%">|</th>
                       <th width="22%">Quantity (tickets)</th>
                       <th width="2%">|</th>
                       <th width="22%">Amount (riels)</th>
                       <th width="8%"> </th>
                     </tr>
                   </tbody></table>
                 </div>
                 <div id="box" style="border:1px solid #ccc;">
                   <table id="fatable" class="datatable" cellpadding="0 " cellspacing="0" width="100%">
                     <tbody>
                     <tr>
                       <td width="22%">
                       	   <select style="width: 230px" name="planCode">
	                       </select>
                       </td>
                       <td width="2%"> </td>
                       <td width="22%"><input type="text"  name="packages"/></td>
                       <td width="2%"> </td>
                       <td width="22%"><input type="text"  name="tickets"/> </td>
                       <td width="2%"> </td>
                       <td width="22%"><input type="text"  name="amount"/> </td>
                       <td width="4%">
                       		<img src="/AddTrDemo/img/add.png"  οnclick="addTr(this);" height="16px" width="16px"/>
                       </td>
                       <td width="4%">
                       		<img src="/AddTrDemo/img/cross.png"  οnclick="delTr(this);" height="16px" width="16px"/>
                       </td>
                     </tr>
                   </tbody></table>
                 </div>
           </div>
		</div>
	</form>

</body>
</html></span>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值