1.效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/4a1d9d53d38f46d980573e2f4267d8e6.png)
2.Js使用jq来解决
4.注意点
由2个table合并完成,最后的确认按钮在下一个table中,不然复制的效果不好。
这个复制是复制最后一行的tr,然后加到最后一行中。应该确保最后一行的tr后面没有其他影响的东西。
5.核心代码
<html>
<body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<div id="tabbody-div">
<form action="a.php" method="post" enctype="multipart/form-data" name="theForm" >
<table cellspacing="0" border="0" style="width:100%;" id="table_main">
<tr>
<td class="label_r" >类型</td>
<td >
衣服
<input type="hidden" name="type_2" id="type_2" class="form-control-ele" value="2" />
</td>
<td class="label_r" >金额</td>
<td >
<input style="width: 80%;height: 28px;" type="text" name="fee_2" id="fee_2" class="form-control-ele" value="{$row.fee_2}" />万
</td>
<td class="label_r">系数</td>
<td colspan="4" style="text-align: left;">
<input style="width: 30%;height: 28px;" type="text" name="rate_2" id="rate_2" class="form-control-ele" value="{$row.rate_2}" />%
</td>
</tr>
<tr>
<td class="label_r" >类型</td>
<td width="8%" >
广东欢迎你
<input type="hidden" name="type_3[]" id="type_3" class="form-control-ele" value="2" />
</td>
<td class="label_r" >最低</td>
<td width="12%">
<input style="width: 80%;height: 28px;" type="text" name="min_fee[]" id="min_fee" class="form-control-ele" value="{$row.min_fee}" />万
</td>
<td class="label_r" >最高</td>
<td width="15%" style="text-align: left;">
<input style="width: 80%;height: 28px;" type="text" name="max_fee[]" id="max_fee" class="form-control-ele" value="{$row.max_fee}" />万
</td>
<td class="label_r">系数</td>
<td style="text-align: left;" width="10%">
<input style="width: 80%;height: 28px;" type="text" name="rate_3[]" id="rate_3" class="form-control-ele" value="{$row.rate_3}" />%
</td>
<td width="5%">
<span id="copy_select" class="select_option" style="color:blue;font-size: 25px;cursor:hand">+</span>
</td>
</tr>
</table>
<table >
<tr>
<td colspan="10" style="text-align:center;">
<input type="submit" class="big_button" value="确认提交" />
<input type="hidden" name="id" value="1" />
</td>
</tr>
</table>
</form>
</div>
</body>
<script>
//复制tr
$(function () {
$(".select_option").click(function (){
//判断复制的个数不能超过8个,原有tr有2个就是头部
var table_tr_lenth = $("#table_main").find("tr").length;
if(table_tr_lenth > 21)
{
alert('最多可复制20个');
return false;
}
var copy_tr = $(this).parents("tr").clone()
copy_tr.appendTo("#table_main");
//清空最后一行的数据
del_clear_last_tr();
//添加删除事件
changeDel();
})
})
//清空最后一行的输入的数据或者复制后的数据
function del_clear_last_tr() {
$('#table_main tr:last').find('input[type=text]').each(function() {
$(this).val('')
});
}
//复制的内容修改class(变为删除的class)和+变-号
function changeDel()
{
//首先获取最后一行,然后修改class
$('#table_main tr:last').find('#copy_select').attr("class","btn_delete")
//绑定删除事件
$('#table_main tr:last').find('#copy_select').attr("onclick","removeTr(this)")
//+变-号
$('#table_main tr:last').find('#copy_select').text('-');
}
//删除事件
function removeTr(e){
$(e).parents("tr").remove();
}
</script>