Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery删除所选元素,动态生成_Javascript_Jquery_Dynamically Generated - Fatal编程技术网

Javascript jQuery删除所选元素,动态生成

Javascript jQuery删除所选元素,动态生成,javascript,jquery,dynamically-generated,Javascript,Jquery,Dynamically Generated,我有一个表单,我可以动态添加更多行,但如果我试图删除一个特定的行,它不起作用,但是第一行被删除没有问题 以下是html: <form class="order-form"> <div class="product-lines"> <!-- Product Line Section --> <div class="product-line"> <a href="#" alt="close" class="

我有一个表单,我可以动态添加更多行,但如果我试图删除一个特定的行,它不起作用,但是第一行被删除没有问题

以下是html:

<form class="order-form">
  <div class="product-lines">
<!-- Product Line Section -->
  <div class="product-line">                
<a href="#" alt="close" class="btn-close" title="Remove"><img alt="remove" src="img/close.png"  /></a>
<input class="input-text" name="product-code" type="text" placeholder="Product Code" ></input>
<input class="input-text" name="product-quantity" type="text" placeholder="Quantity"></input>
<input class="input-text" name="product-discript" type="text" placeholder="Discription of Product"></input>
<label class="label-sign">&pound;</label>
<input class="input-text" name="product-price" type="text" placeholder="RRP Price"></input>
<br>
  </div>
</div>

<div id="product-btn">                  
<input name="btn-add-line" type="button" value="Add new line"></input>
<input name="btn-update" type="button" value="Update"></input>
<input name="btn-submit" type="submit" value="Submit Order"></input>
<label class="label-sign">&pound;</label>
<input class="input-text" name="order-info" type="text" placeholder="Order total" ></input>
</div>                  
</form>
我也试过了

$(e.currentTarget).parents("div.product-lines).next("div.product-line).remove();

任何帮助都将不胜感激,解释也将对我的学习非常有帮助。

试试类似的方法

$(".product-lines").on("click", ".btn-close", function(e){

    $(e.currentTarget).parent().remove();   

});
不能将事件附加到当前不在页面(行)上的对象。
您必须在产品线对象上附加单击事件,当单击该事件时,您将事件委托给“最近的”产品线对象

试试类似的方法

$(".product-lines").on("click", ".btn-close", function(e){

    $(e.currentTarget).parent().remove();   

});
不能将事件附加到当前不在页面(行)上的对象。
您必须在产品线对象上附加单击事件,当单击该事件时,您将事件委托给“最近的”产品线对象

您将需要稍微更改jQuery以允许。这意味着将来添加的所有元素也将附加事件

$(document).on("click", ".btn-close", function(e){

   $(e.currentTarget).parent().remove();   

}); 

您将需要稍微更改jQuery以允许。这意味着将来添加的所有元素也将附加事件

$(document).on("click", ".btn-close", function(e){

   $(e.currentTarget).parent().remove();   

}); 
但是如果您使用此代码,它将删除

为什么要删除此分区

我不太明白你的问题。一步一步地详细解释

但是如果您使用此代码,它将删除

为什么要删除此分区


我不太明白你的问题。详细解释并逐步解释。

-so
$(文档)。on('click','.btn close',function(){…})
-so
$(文档)。on('click','.btn close',function(){…})
@Arun p Johny Reading event now,谢谢@Zinox很有趣,但不是授权degradation@elrado如何防止第一行现在被删除(基本上在页面上保留一行)嗯,不要在其附近添加btn。@Arun P Johny Reading event Degradization now,谢谢@Zinox很有趣,但不是授权degradation@elrado如何避免第一行被删除(基本上在页面上保留一行)嗯,不要在靠近它的地方添加btn。谢谢!我只是在读事件降级,你是对的,我只是意识到我不想删除主线。关于如何不删除主要内容的任何想法。我希望这个问题有道理。谢谢!我只是在读事件降级,你是对的,我只是意识到我不想删除主线。关于如何不删除主要内容的任何想法。我希望这个问题有意义。谢谢你的帮助!我刚刚读了一些关于事件降级的文章。谢谢你的帮助!我只是在读有关事件降级的文章。