简体   繁体   English

我必须通过jQuery删除父div

[英]I have to remove parent div through jQuery

I have to remove whole div which having class "input-group" . 我必须删除有“输入组”类的整个div。 I have to do this by jQuery. 我必须通过jQuery来做到这一点。 I tried through jQuery but div is not removing. 我试过jQuery,但div没有删除。

Here is my code, 这是我的代码,

 $(document).ready(function() { $(".wrapper").on('click', '.rmvBtn', function(e) { e.preventDefault(); $(this).parent('div').remove(); x--; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="input-group"> <input type="text" class="form-control" name="field_name[]" value="" /> <span class="input-group-addon"> <a href="#" class="rmvBtn" title="Remove field""> <span class="glyphicon glyphicon-minus"></span> </a> </span> </div> </div> 

Please let me know what errors have in my code ? 请告诉我代码中有哪些错误? Thanks in advance. 提前致谢。

Can you please try below code: 你可以试试下面的代码:

$(document).ready(function(){

    $(".wrapper").on('click', '.rmvBtn', function(e){ 
          e.preventDefault();
          $(this).closest('div.input-group').remove();
          x--;
    });

});

$.closest is for traversing up through its ancestors in the DOM tree till we get the match. $ .closest用于遍历DOM树中的祖先,直到我们得到匹配。

In your code; 在你的代码中; rmvBtn is inside <span> tag. rmvBtn位于<span>标记内。 Hence $(this).parent('div') returns you nothing as $.parent() traverse only single level up in DOM tree. 因此$(this).parent('div')返回任何内容,因为$.parent()仅在DOM树中遍历单个级别。

Use closest instead of parents and you will be fine. 使用closest而不是parents ,你会没事的。
And your HTML is wrong, the a tag has two "" at the last attribute. 而你的HTML是错误的, a标签在最后一个属性上有两个""

 $(function() { $(".wrapper").on('click', '.rmvBtn', function(e) { e.preventDefault(); $(this).closest("div").remove(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="input-group"> <input type="text" class="form-control" name="field_name[]" value="" /> <span class="input-group-addon"> <a href="#" class="rmvBtn" title="Remove field"> <span class="glyphicon glyphicon-minus">btn</span> </a> </span> </div> </div> 

尝试使用parents()代替parent()

$(this).parents('.input-group').remove();

Use closest to find the closest element with a specified selector. 使用“ closest可查找具有指定选择器的最近元素。 The closest element is found by bubbling up from the current element. 通过从当前元素冒泡找到最接近的元素。

Also use the div class selector to be safe while removing the required div in case of additional div's being added into your HTML 如果将额外的div添加到HTML中,也可以在删除所需的div时使用div类选择器是安全的

$(this).closest('div.input-group').remove();

 $(document).ready(function() { $(".wrapper").on('click', '.rmvBtn', function(e) { e.preventDefault(); $(this).closest('div.input-group').remove(); // x--; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="input-group"> <input type="text" class="form-control" name="field_name[]" value="" /> <span class="input-group-addon"> <a href="#" class="rmvBtn" title="Remove field""> <span class="glyphicon glyphicon-minus">remove</span> </a> </span> </div> </div> 

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

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