动态删除div的错误

时间:2016-10-07 02:12:12

标签: javascript jquery

我试图每行动态添加3个字段(2个文本字段和1个删除按钮)。添加按钮工作正常。问题是删除功能。当我添加2个或更多字段时,我尝试删除第一个字段,删除所有添加的字段/行

这是我的HTML代码:

<div class="col-lg-12">

    <div class="field_wrapper form-group col-sm-12">
        <div class="form-group col-sm-4">
            <input type="text" name="id[]" value="" class="form-control"/>
        </div>
        <div class="form-group col-sm-4">
            <input type="text" name="name[]" value="" class="form-control"/>
        </div>
        <div class="form-group col-sm-4">
            <a href="javascript:void(0);" class="add_button btn btn-primary" title="Add field">Adicionar Valor</a>
        </div>
    </div>

</div>

这是javascript:

<script type="text/javascript">
$(document).ready(function(){
    var maxField = 10; //Input fields increment limitation
    var addButton = $('.add_button'); //Add button selector
    var wrapper = $('.field_wrapper:last'); //Input field wrapper
    var wrapper_delete = $('.field_wrapper');

    //var fieldHTML = '<div><input type="text" name="field_name[]" value=""/><a href="javascript:void(0);" class="remove_button btn btn-danger2" title="Remove field">Remover</a></div>'; //New input field html 
    var fieldHTML = '<div class="field_wrapper form-group col-sm-12"><div class="form-group col-sm-4"><input type="text" name="id[]" value="" class="form-control"/></div><div class="form-group col-sm-4"><input type="text" name="name[]" value="" class="form-control"/></div><div class="form-group col-sm-4"><a href="javascript:void(0);" class="remove_button btn btn-danger2" title="Add field"><i class="fa fa-trash-o"></i></a></div></div>'; //New input field html 
    var x = 1; //Initial field counter is 1
    $(addButton).click(function(){ //Once add button is clicked
        wrapper = $('.field_wrapper:last'); //Input field wrapper
        if(x < maxField){ //Check maximum number of input fields
            x++; //Increment field counter
            var count = $('.field_wrapper').length;
            fieldHTML = '<div class="field_wrapper form-group col-sm-12"><div class="form-group col-sm-4"><input type="text" name="id_'+ count +'" value="" class="form-control"/></div><div class="form-group col-sm-4"><input type="text" name="name_' + count +'" value="" class="form-control"/></div><div class="form-group col-sm-4"><a href="javascript:void(0);" class="remove_button btn btn-danger2" title="Add field"><i class="fa fa-trash-o"></i></a></div></div>'; //New input field html
            $(wrapper).append(fieldHTML); // Add field html

        }
    });

    $(wrapper_delete).on('click', '.remove_button', function(e){ //Once remove button is clicked
        wrapper_delete = $('.field_wrapper');
        e.preventDefault();
        $(this).closest('.field_wrapper').remove();
        x--; //Decrement field counter
    });
});
</script>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您要将新的field_wrapper元素附加到field_wrapper:last。您应该将放在之后,因此您应该使用.append()代替.after()

$(wrapper).after(fieldHTML); // Add field html

并在document(或最近的静态祖先)上委托点击事件,因为.field_wrapper元素也是动态添加的,您无法将事件附加到它。

$(document).on('click', '.remove_button', function(e){ 

JSFiddle

看看你想做什么。我建议使用.clone([with events])。您实际上不需要更改input名称(除非原因不是避免重复名称),因为它们设置为 arrays
这样,您的所有代码都可以如下所示:

$('.add_button').click(function(){
  $('.field_wrapper').length >= maxField ||
    wrapper.clone(true).find('a.btn').toggle().end().insertAfter('.field_wrapper:last');
});

$('.remove_button').hide().click(function(){
  $(this).closest('.field_wrapper').remove();
});

var maxField = 10, wrapper = $('.field_wrapper').clone(true);

JSFiddle