jquery动态添加删除form元素

效果图:

点击+,在其后会添加一条记录,点击-,会删除-号所在的记录

html:

<div class="form-group">
        <label class="col-sm-2 control-label">分集名称:</label>
        <div class="col-sm-3">
            <input th:name="name" class="form-control">
        </div>
        <label class="col-sm-2 control-label">节目文件:</label>
        <input class="col-sm-1" name="file1" type="file" value="选择" size="20" id="file11" onchange
                    ="uploadVideo('Video')">
        <button type="button" class="add_node btn btn-primary">+</button>
 </div>

js:

$(function () {
    $(document).on("click",".add_node",function () {
        $node ='<div class="form-group">\n' +
            '                            <label class="col-sm-2 control-label">分集名称:</label>\n' +
            '                            <div class="col-sm-3">\n' +
            '                                <input th:name="name" class="form-control">\n' +
            '                            </div>\n' +
            '                            <label class="col-sm-2 control-label">节目文件:</label>\n' +
            '                            <input name="file1" class="col-sm-1" type="file" value="选择" size="20" id="file11" onchange\n' +
            '                                        ="uploadVideo(\'Video\')">\n' +
            '<button type="button"  class="add_node btn btn-primary" >+</button>'+
            '                            <button type="button" class="del_node btn btn-warning" >-</button>\n' +
            '                        </div>';

        $(this).parent().after($node);
    });
    $(document).on("click",".del_node",function(){
        $(this).parent().remove();
    })
});

ps:jquery生成html标签时无法再通过jquery去获取,需要用on来绑定。

$(document).on("click",'.add_node',function(){//需要这样写 });

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值