最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery remove cloned element on click - Stack Overflow

programmeradmin2浏览0评论

I have a script that removes the duplicated form when clicked. But when you hit remove I want to only remove the form the was cloned. I believe i need to use $this but not sure how?

jQuery

$(".remove").click(function() {
    $('.duplicate').remove();
});

HTML

<div class="duplicate">
    <p>Form Duplicate</p>
    <a href="#" class="add">Add Guest</a> | <a href="#" class="remove">Remove </a>
</div>
<div class="duplicate">
    <p>Form Duplicate</p>
    <a href="#" class="add">Add Guest</a> | <a href="#" class="remove">Remove </a>
</div>

Here's a live preview: /.

Thanks!

I have a script that removes the duplicated form when clicked. But when you hit remove I want to only remove the form the was cloned. I believe i need to use $this but not sure how?

jQuery

$(".remove").click(function() {
    $('.duplicate').remove();
});

HTML

<div class="duplicate">
    <p>Form Duplicate</p>
    <a href="#" class="add">Add Guest</a> | <a href="#" class="remove">Remove </a>
</div>
<div class="duplicate">
    <p>Form Duplicate</p>
    <a href="#" class="add">Add Guest</a> | <a href="#" class="remove">Remove </a>
</div>

Here's a live preview: http://www.waterfrontexeter.co.uk/preordernew/.

Thanks!

Share Improve this question edited Mar 21, 2013 at 14:21 VisioN 145k34 gold badges287 silver badges289 bronze badges asked Mar 21, 2013 at 13:57 BrentBrent 2,48510 gold badges41 silver badges64 bronze badges 1
  • Here's my answer for [remove clone][1] [1]: stackoverflow./questions/6985899/… – Ravi Kant Commented Oct 15, 2013 at 6:08
Add a ment  | 

1 Answer 1

Reset to default 6

Just use closest() and don't forget about preventDefault():

$(".remove").click(function(e) {
    $(this).closest(".duplicate").remove();
    e.preventDefault();
});
发布评论

评论列表(0)

  1. 暂无评论