简体   繁体   English

动态追加div并在jquery中删除相同的div

[英]Dynamically append divs and remove the same div in jquery

I have got stuck somewhere. 我被困在某个地方。 Expecting your help seriously. 期待您的帮助。

I am trying to append divs dynamically with "id" s and remove the same div with the same "id" on click a button. 我试图用“ id” s动态附加div并在单击按钮时删除具有相同“ id”的相同div。 Everything is working fine. 一切正常。 When I am clicking the relevant button to append div, divs are appending with dynamic id. 当我单击相关按钮以附加div时,div将附加动态ID。 Problem is on removing the same div on click a "Close" button inside the appended div. 问题是在单击附加div内的“关闭”按钮后删除相同的div。

Suppose my appended divs are "Order2" and "Order3". 假设我附加的div是“ Order2”和“ Order3”。 When I click a "Close" button inside the "Order3" appended div, it is removing the same div ie"Order3", but before removing the "Order3" div if I click on the "Close" button inside the "Order2" div all the appended divs are removed. 当我单击“ Order3”附加div中的“关闭”按钮时,它将删除相同的div,即“ Order3”,但是如果我单击“ Order2” div中的“关闭”按钮,则在删除“ Order3” div之前删除所有附加的div。 This is not expected. 这是不期望的。 It is expected when I click the "Close" button inside the "Order2" div it will remove the "Order2" div. 可以预期,当我单击“ Order2” div内的“关闭”按钮时,它将删除“ Order2” div。

I know I am doing silly mistake. 我知道我在做愚蠢的错误。 Please help me to sort out. 请帮我整理一下。 Thanks in advance My js Code is: 预先感谢我的js代码是:

$(document).ready(function(){
  function addallTit() {
   var container = $('#addTitles');
   var inn = container.find('.app-div');

   var $id = inn.length+1;
   $id++;
$(container).append('<div id="app'+$id+'"><div class="col-md-12"><div class="app-div"><div class="closed-btn pull-right"><a href="javascript:"><i class="fa fa-times "></i></a></div> <div class="input-group" > <span id="sizing-addon2" class="input-group-addon">Order # '+ $id +'</span><input type="text" placeholder=""  class="form-control">  </div></div></div></div>');

$('.closed-btn ').click(function(e){
     e.preventDefault();
    $('#app'+$id).remove();


 });

};


$('#titlesAdds').click(function() {
    addallTit();
    });



});

Here is the link : Fiddle 这是链接: 小提琴

Simply change event for close as it is dynamic element. 只需将事件更改为关闭,因为它是动态元素。

container.on('click','.closed-btn',function(e){
         e.preventDefault();
         $('#app'+$id).remove();

 });

One of your problem is, you want to remove content that is dynamically added. 问题之一是,您想删除动态添加的内容。 If you want to remove added element, using .on method and target the parent of element as selector. 如果要删除添加的元素,请使用.on方法并将目标父元素作为选择器。 Then the second problem is, jquery did't know which element you want to remove. 然后第二个问题是,jquery不知道要删除哪个元素。 The idea is, find the parent of current close link, and remove. 这个想法是,找到当前关闭链接的父级,然后删除。 Simply change your code into this : 只需将您的代码更改为此:

$('#addTitles').on('click','.closed-btn',function(e){
   e.preventDefault();
   var par = $(this).parent();
   $(par).remove();

});  

i am making a modification to Norlihazmey Ghazali's answer. 我正在修改Norlihazmey Ghazali的答案。 But this modification would properly remove all components of the div that you have appended. 但是,此修改将正确删除您已附加的div的所有组件。

$('#addTitles').on('click','.closed-btn',function(e){

   e.preventDefault();

   var par = $(this).closest($("[id^=app]"));

   $(par).remove();

}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM