jquery添加和删除unbind

时间:2013-07-12 08:20:18

标签: jquery jquery-ui

我正在使用jquery做u.i.问题是,我创建了一个容器,其内部有添加 按钮删除

如果我点击add,它只会附加到另一个div一次然后。

如果我单击附加div上的remove它将被删除,它可以再次添加到另一个div,就像删除了事件处理程序一样。

我已经尝试了.unbind,但它对我的代码无效。

请用我的代码帮助我:

http://jsfiddle.net/8mNKN/

$(".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>

1 个答案:

答案 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