【问题标题】:Making a jQuery UI droppable only accept one draggable at a time使 jQuery UI 可放置一次只接受一个可拖动的
【发布时间】:2012-04-26 17:24:56
【问题描述】:

我只在其他几个地方看到过这个问题的几个变体,特别是 herehere

基本上,我有一个跳棋棋盘,棋盘上的每个方块都是可放置的,每个棋子都是可拖动的。每个方块一次只能有一块,我正在尝试根据方块上是否有一块来切换启用/禁用方法。

这是我目前所获得的链接:http://jsbin.com/ayalaz,下面是最相关的代码。

function handleDrop(e, ui) {
            var tileNumber = $(this).data('tile');
            // Make the gamepiece snap into the tile
            ui.draggable
                .data({ // WHAT IF PIECE IS SET BACK DOWN IN SAME TILE... CHECK FOR IT!
                    'preRow': ui.draggable.data('curRow'),
                    'preCol': ui.draggable.data('curCol'),
                    'curRow': $(this).data('row'),
                    'curCol': $(this).data('col')
                });
            $(this).append($(ui.draggable));
            ui.draggable
                .position({
                    of: $(this),
                    my: 'left top',
                    at: 'left top'
                });
            $(this).droppable('disable');
            //console.log("Gamepiece set down at: (" + $(this).data('row') + "," + $(this).data('col')+ ")");
        }

function handleOut(e, ui) {
            // HOW TO TOGGLE DROPPABLE??
            $(this).droppable('enable');
        }

有什么建议吗?

提前致谢! 杰里米

【问题讨论】:

    标签: jquery-ui jquery-ui-draggable jquery-ui-droppable


    【解决方案1】:

    看起来您在第一次放置事件后成功禁用了磁贴上的放置。您的问题是您最初没有在初始占用的磁贴集上将 droppable 设置为禁用。

    在您创建游戏板和棋子之后,假设我的 CSS 选择器准确地反映了您的结构,这应该可以完成:

    $('div.gamePiece').parent().droppable("option", "disabled", true);
    

    请注意,这与更改初始选项中可放置性的语法不同。来自jQuery UI droppable documentation

    //initialize
    $( ".selector" ).droppable({ disabled: true });
    
    //setter
    $( ".selector" ).droppable( "option", "disabled", true );
    

    编辑

    看来我对$(this).droppable('disable');$(this).droppable('enable'); jquery-ui 的看法是错误的确实 具有启用和禁用的别名功能。

    enable: function() {
        return this._setOption( "disabled", false );
    },
    disable: function() {
        return this._setOption( "disabled", true );
    },
    

    【讨论】:

    • 感谢@pabo 的回答。您的代码确实将棋盘图块初始化为“禁用”,但是一旦它们已经包含游戏块,droppable 的“dropout”事件(我的函数名为“handleOut”)仍然不会重新激活任何图块。
    • @minttoothpick:$(this).droppable('disable');$(this).droppable('enable'); 不工作。使用上面我回答的“setter”语法:$( ".selector" ).droppable( "option", "disabled", true );
    • 我能够通过使用原始的$(this).droppable('disable'); 方法获得我的代码功能(在我看到你的最后一条评论之前)......我的问题是我需要将禁用/启用切换到dropover 事件。它确实看起来像$(this).droppable('disable'); 在实际的drop 事件中永远不会起作用。为什么会这样?再次感谢你的帮助!我真的很感激。
    猜你喜欢
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2014-06-16
    • 1970-01-01
    相关资源
    最近更新 更多