删除附加的html代码

时间:2018-02-01 08:19:26

标签: javascript jquery html

我目前遇到了一个问题,我无法用jQuery删除附加的html。

现在有效的是我可以附加我想要的html,看起来不错,但删除不起作用。它仅适用于重新加载第一个加载的html,其他我追加的不起作用 - 我不知道为什么。

这是我附加html的代码:

$("#adWasteStream").click(function(){
    var newTxt = $('<div class="row-waste-container width-100"><div class="row"><div class="col-md-9"><p>Stream 1</p></div><div class="col-md-3"><button class="btn btn-add-waste deleteWasteStream"><i class="fa fa-minus-circle o-btn-add" aria-hidden="true"></i>Remove Waste Stream</button></div><div class="form-group col-md-7"><label for="business-mng-name" class="control-label">Waste Stream</label><input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="Co-mingled recycling"><span class="help-block"></span></div></div><div class="row width-100"><div class="form-group col-md-4"><label for="business-mng-name" class="control-label">Volume (m <sup>3</sup>)</label><input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="50"><span class="help-block"></span></div><div class="form-group col-md-4"><label for="business-mng-name" class="control-label">Weight (t)</label><input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="20"><span class="help-block"></span></div><div class="form-group col-md-4"><label for="business-mng-name" class="control-label">Cost ($)</label><input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="400"><span class="help-block"></span></div></div></div>');
    $(".row-cont-main").append(newTxt);
});

我的删除代码:

$(".deleteWasteStream").click(function(){
      var curRow = $(this).parents('div.row-waste-container');
      curRow.remove();
});

我不需要使用Id,因为它们需要是唯一的所有元素(用于后端目的)。

html的结构如下所示:

<div class="row-waste-container width-100">
                                    <div class="row">
                                        <div class="col-md-9">
                                            <p>Stream 1</p>
                                        </div>
                                        <div class="col-md-3">
                                            <button class="btn btn-add-waste deleteWasteStream"><i class="fa fa-minus-circle o-btn-add" aria-hidden="true"></i>Remove Waste Stream</button>
                                        </div>
                                        <div class="form-group col-md-7">
                                            <label for="business-mng-name" class="control-label">Waste Stream</label>
                                            <input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="Co-mingled recycling">
                                            <span class="help-block"></span>
                                        </div>
                                    </div><!-- end row -->
                                    <div class="row width-100">
                                        <div class="form-group col-md-4">
                                            <label for="business-mng-name" class="control-label">Volume (m <sup>3</sup>)</label>
                                            <input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="50">
                                            <span class="help-block"></span>
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label for="business-mng-name" class="control-label">Weight (t)</label>
                                            <input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="20">
                                            <span class="help-block"></span>
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label for="business-mng-name" class="control-label">Cost ($)</label>
                                            <input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="400">
                                            <span class="help-block"></span>
                                        </div>
                                    </div><!-- end row -->
                                </div><!-- end row-waste container -->

3 个答案:

答案 0 :(得分:2)

您需要更改Click事件处理程序以处理动态附加的html。

使用以下代码更新您的移除点击处理程序

$("body").on('click' , '.deleteWasteStream' , function(){
      var curRow = $(this).parents('div.row-waste-container');
      curRow.remove();
});

答案 1 :(得分:1)

您无法在动态对象/ html上使用.click()。您可以使用.on()

$("body").on('click', '.deleteWasteStream', function( event ) {
    var curRow = $(this).parents('div.row-waste-container');
    curRow.remove();
});

文档:http://api.jquery.com/on/

答案 2 :(得分:0)

由于元素是动态创建的,因此它们不像预渲染元素(在浏览器中加载网页时渲染的元素)那样在DOM中注册。最好的解决方案是使用on事件绑定器

将动态创建的元素绑定到预渲染的元素
$('.row-cont-main').on('click','.deleteWasteStream',function(){
     var curRow = $(this).parents('div.row-waste-container');
     curRow.remove();
});