Draggables Jquery UI, disable individual div on droppable

时光毁灭记忆、已成空白 提交于 2019-12-06 09:58:59

问题


I'm trying to make some educational site and one of the things I want to have the students do is drag items to an area before being able to proceed to the next stage.

I was thing the way to do this was use the jquery droppables script and have the items draggable option disable when it is dropped in the droppable div. Then I was going to add 1 to a variable, when it gets to the right number the button to advance would be enabled.

The problem is that I can't figure out how to make the droppable function work only for each particular draggable. So what I have is:

$(document).ready(function() {
    $("#draggable").draggable();
    $("#draggable2").draggable();
    $("#droppable").droppable({
      drop: function() { $( "#draggable" ).draggable( "option", "disabled", true );; }
    });
  });

With these divs;

<div id="droppable">Drop here</div>
<div id="draggable" class="drag">Drag me</div>
<div id="draggable2" class="drag">Drag me</div>

But of course, all this does is disable the the first draggable whenever any are dragged into the droppable div. My javascript is not very good (im learning) and I don't know how I would make it only disable the one that I put into the droppable area.

Can anyone help please?


回答1:


You're just putting the right code in the wrong place.

$( "#draggable" ).draggable( "option", "disabled", true )

The above will work on it's own and turn off draggable for #draggable, no need to put it in the drop function.

OR:

$("#droppable").droppable({
  disabled: true
});

Either will work.




回答2:


This can be achieved by defining a scope to both draggables and droppable area:

$(function() {
    $('.drag').draggable({
        revert: "invalid",
        scope: "items"
    });
    $('#droppable').droppable({
        scope: "items"
    });
});

Example Link.



来源:https://stackoverflow.com/questions/4564865/draggables-jquery-ui-disable-individual-div-on-droppable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!