Jquery删除上面的br标记

Jquery remove br tag on above

本文关键字:br 标记 删除 Jquery      更新时间:2023-09-26

我创建了一个可以添加和删除的动态字段。

但在我的脚本中,删除按钮是删除输入字段和所有br行。

我希望它只删除我点击的

var max_fields      = 5; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<br>');
            $(wrapper).append('<div><input type="text" name="meaning[]" class="form-control" /><a href="#" class="remove_field"> <span class="glyphicon glyphicon-trash" aria-hidden="true" id="trash"></span></a></div>'); //add input box
        }
    });
    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault();
        $(this).parent('div').remove(); x--;
         $("br").remove();
    })

当我单击"删除"时,上面的代码将删除所有br。

如果我只想删除父div,该怎么办?

尝试使用prev()函数:

// Store $(this) to a variable in order not to call jQuery multiple times
var that = $(this);
that.parent('div').remove(); x--;
that.prev('br').remove();

检查文档