我正在使用jquery做u.i.问题是,我创建了一个容器,其内部有添加 按钮和删除。
如果我点击add
,它只会附加到另一个div一次然后。
如果我单击附加div上的remove
它将被删除,它可以再次添加到另一个div,就像删除了事件处理程序一样。
我已经尝试了.unbind,但它对我的代码无效。
请用我的代码帮助我:
$(".more").one("click",function(e) {
$(this).parent().clone().appendTo("#we");
});
$(document).on("click","#we .remove",function() {
$('.more').unbind('click.MyNamespace');
$(this).parent(".container").remove();
});
<div class="container">
<p>There are 0 boxes</p>
<a href="#" class="more">Add more +</a>
<a href="#" class="remove">Add more +</a>
</div>
<br/>
<div class="container">
<p>There are 0 boxes</p>
<a href="#" class="more">Add more +</a>
<a href="#" class="remove">Remove -</a>
</div>
<br/>
<div class="container">
<p>There are 0 boxes</p>
<a href="#" class="more">Add more +</a>
<a href="#" class="remove">Remove -</a>
</div>
<br/>
<div id="we">
</div>
答案 0 :(得分:0)
如果我理解你的要求,那么
$(".more").on("click",function(e) {
var ct = $(this).closest(".container");
if(ct.data('cloned')){
return;
}
ct.clone().appendTo("#we").data('source', ct);
ct.data('cloned', true);
});
$(document).on("click","#we .remove",function() {
var ct = $(this).closest(".container");
ct.data('source').data('cloned', false)
ct.remove();
});
演示:Fiddle