动态表单添加一行,删除一行

效果图

 

html部分

<form class="cus-form" id="form">
<div class="padding20 bgWhite marginTop20">
<div class="container form-horizontal form-box">
<div id="enuParams">

</div>
</div>
</div>
</form>

js部分
<script type="text/javascript">

var enuParam_index = 1;
$(function() {

var enuParam = "<div id='enuParam_"+enuParam_index+"' class='row'>" +
"<div class='form-item col-sm-5 topAndBottom'>" +
"<label class='col-sm-6 control-label'> 枚举值: </label>" +
"<div class='col-sm-6'>" +
"<input type='text' class='form-control input-sm value_a' placeholder='请填写枚举值' name='enu_value' maxlength='32'>" +
"</div>" +
"</div>" +
"<div class='form-item col-sm-4 topAndBottom'>" +
"<label class='col-sm-4 control-label'>枚举名称: </label>" +
"<div class='col-sm-8'>" +
"<input type='text' class='form-control input-sm' placeholder='请填写枚举名称' name='enu_name' maxlength='32'>" +
"</div>" +
"</div>" +
"<div class='form-item col-sm-2 topAndBottom'>" +
"<div class='col-sm-12'>" +
"<span id='add' type='button' class='btn stockBtn'>+</span>" +
"<span οnclick='del_enuParam(id)' id='del_enuParam_"+enuParam_index+"' type='button' class='btn stockBtn'>-</span>" +
"</div>" +
"</div>" +
"</div>";
$("#enuParams").append(enuParam);

enuParam_index++;
});

//添加配置枚举
// var addrow = document.getElementById("enuParam").outerHTML;

$(document).on("click", "#add", function(){
var enuParam = "<div id='enuParam_"+enuParam_index+"' class='row'>" +
"<div class='form-item col-sm-5 topAndBottom'>" +
"<label class='col-sm-6 control-label'> 枚举值: </label>" +
"<div class='col-sm-6'>" +
"<input type='text' class='form-control input-sm value_a' placeholder='请填写枚举值' name='enu_value' maxlength='32'>" +
"</div>" +
"</div>" +
"<div class='form-item col-sm-4 topAndBottom'>" +
"<label class='col-sm-4 control-label'>枚举名称: </label>" +
"<div class='col-sm-8'>" +
"<input type='text' class='form-control input-sm' placeholder='请填写枚举名称' name='enu_name' maxlength='32'>" +
"</div>" +
"</div>" +
"<div class='form-item col-sm-2 topAndBottom'>" +
"<div class='col-sm-12'>" +
"<span id='add' type='button' class='btn stockBtn'>+</span>" +
"<span οnclick='del_enuParam(id)' id='del_enuParam_"+enuParam_index+"' type='button' class='btn stockBtn'>-</span>" +
"</div>" +
"</div>" +
"</div>";
$("#enuParams").append(enuParam);
enuParam_index++;


});

function del_enuParam(id){
var aa = id.split("_")
var ids = aa[1]+"_"+aa[2]
//alert(id)
//alert(ids)
var enuParams = document.getElementById("enuParams")
if(enuParams.childNodes.length>2){
enuParams.removeChild(document.getElementById(ids))
}else{
layer.alert("至少保留一个");
}
}


</script>
 -----------------------------------------------

删除一行的另一种写法

$(document).on("click", "#delete_1_0", function(){
              if($("#param_1_1_0 .row").length=="1"){
        return;
    }else{
      if($(this).parent().parent("#param_1_1_1_0:first-child").attr("id")=="param_1_1_1_0"){
        return;
      }else{
        $(this).parent().parent().remove();
        }
      }
  });

 

转载于:https://www.cnblogs.com/required/p/10443459.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值