Draggables Jquery UI,禁用可放置的单个 div

发布于 2024-10-09 22:30:47 字数 794 浏览 2 评论 0原文

我正在尝试制作一些教育网站,我想让学生做的一件事是将项目拖到一个区域,然后才能进入下一阶段。

我认为执行此操作的方法是使用 jquery droppables 脚本,并在将项目拖放到可放置 div 中时禁用可拖动选项。然后我将向变量添加 1,当它达到正确的数字时,将启用前进按钮。

问题是我无法弄清楚如何使可放置功能仅适用于每个特定的可拖动对象。所以我所拥有的是:

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

有了这些 div;

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

但当然,这一切所做的只是在将任何拖拽到可放置 div 中时禁用第一个可拖拽。我的 JavaScript 不是很好(正在学习),我不知道如何让它只禁用我放入可放置区域的 JavaScript。

有人可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向日葵 2024-10-16 22:30:47

您只是将正确的代码放在错误的位置。

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

上面的代码将独立工作,并关闭 #draggable 的可拖动功能,无需将其放在放置函数中。

或者:

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

两者都可以。

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.

零度℉ 2024-10-16 22:30:47

这可以通过定义可拖动和可放置区域的范围来实现:

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

示例链接

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文