2011-12-02 97 views
0
<div class="transactionsWrapper"> 
    <input type="button" value="Delete" /> 
</div> 

如果我克隆上面的div让我们说5次,根据删除按钮被点击删除div的jQuery代码是什么?删除克隆的div

回答

1
$(".transactionsWrapper input:button").click(function() { 
    $(this).parent().remove(); 
}); 
+0

它不会工作,因为他有动态div – Distdev

-1

只是...删除输入的父母?

$('input[value=Delete]').on('click', function() { 
    $(this).parent().remove(); 
}); 

编辑

嗯......如果你添加分配事件侦听器后,克隆<div>元素这是行不通的。最安全的方法是使用委托:

$(document.body).on('click', 'input[value=Delete]', function() { 
    $(this).parent().remove(); 
}); 
+0

不知道你的理解很正确的问题。 –

+0

@Ben是关于早期版本答案的评论?因为,就目前而言,它看起来完全像你的答案。 – sdleihssirhc

+0

是的,这是一个旧版本。 –

1

这应该工作:

$('input:button[value="Delete"]').click(function() { 
    $(this).closest('div.transactionsWrapper').remove(); 
}); 
+0

这个解决方案有诀窍。 – JONxBLAZE

1
$(document).on('click', '.transactionsWrapper button', function(){ 
    $(this).parent().remove(); 
}) 
+1

如果jQuery版本低于1.7,只需要将'on'函数替换为'delegate'。 – gustavotkg

0
$(".button").click(function(){ 
    if($(this).parent().hasClass("transactionsWrapper")){ 
     $(this).parent().remove(); 
    } 
})