jQuery禁用/启用Droppable / Draggable的状态

时间:2009-11-19 00:49:43

标签: jquery drag-and-drop photo

我昨天很难解释这一点,但我不认为人们理解它。 我在http://www.whatevercorp.com/problem/

上发布了代码和示例

我有一段时间没有使用过jQuery,但是到处寻找禁用('启用')/禁用示例或者动态更改droppable状态的方法。

基本上我有两个问题:

  1. 如何更改或限制.drop类只接受div中的1个项目 如果你看一下这个例子(whatevercorp.com/problem/),你会看到你可以在一个div中丢弃2个项目,它会变得很时髦。我只是想限制它,因为它是一个照片库。

  2. 如何跟踪表格中的所有可丢弃项目。我希望用户能够在将它们放入框中时将它们移动,但也可以跟踪每个移动。我查看了序列化,但需要一些指针。

  3. 希望清除它,我可以启动并运行此工具。感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

要防止多个图像掉入一个div.drop,请尝试:

$(".drop").droppable({
    accept: ".photo",
    activeClass: 'droppable-active',
    hoverClass: 'droppable-hover',
    drop: function(ev, ui) {
        //returning false will cancel the drop
        if ($(this).children('img').length > 0) return false;

        $(this).append(ui.draggable);   
        $('#output').append($(ui.draggable).attr('src')+"<br />");              
        $('#output').append($(this).attr("id")+"<br />");
    }
});

为了跟踪他们的移动,你可能不得不使用在上面的drop函数中设置的全局变量。