【问题标题】:jquery ui out: function not triggeringjquery ui out:函数未触发
【发布时间】:2015-07-10 12:07:28
【问题描述】:

在 jquery-ui 文档中它说 out: 函数是在将接受的可拖动对象拖出可放置对象时触发的。但在我的情况下,我希望容器都是可拖放的。另外,我希望 id 为 droppable 的 div 也可以排序。现在拖放工作正常,但是当我拖出其中一项时,不会触发 out 功能,所以我该怎么做。

html

<div id="droppable" class="draggable droppable sortable">
</div>
<div id="draggable" class="droppable draggable sortable">
    <p id="hello11" class="item">Hello1</p>
    <p id="hello22" class="item">Hello2</p>
    <p id="hello33" class="item">Hello3</p>
    <p id="hello44" class="item">Hello4</p>
    <p id="hello55" class="item">Hello5</p>
</div>
<div id="form-container">
    <form method="post" action="">
        <!-- Do stuff here -->
        <input type="text" id ="hello1" name="xoxo1" value="">
        <input type="text" id ="hello2" name="xoxo2" value="">
        <input type="text" id ="hello3" name="xoxo3" value="">
        <input type="text" id ="hello4" name="xoxo4" value="">
        <input type="text" id ="hello5" name="xoxo5" value="">
    </form>
</div>

script.js

jQuery(document).ready(function($){
    $( ".droppable" ).droppable({
        accept: ".item"
    });
    $( "#droppable" ).droppable({
      accept: ".item",
      drop: function( event, ui ) {
        // Set values
        var myid = ui.draggable.attr("id").toString();
        myid = myid.substring(0, myid.length - 1);
        document.getElementById(myid).value = myid;
      },
      out: function(event,ui){
        // Unset the values
        var myid = ui.draggable.attr("id").toString();
        myid = myid.substring(0, myid.length - 1);
        document.getElementById(myid).value = '';
      }
    });

    $( ".sortable" ).sortable();

    $( ".draggable .item" ).draggable({
      connectToSortable: ".sortable"
    });
});

Here is a fiddle for that

【问题讨论】:

  • 你会为此提供小提琴吗?
  • 对不起,我在 jsfiddle 上没有这个,我正在本地机器上工作。 @ParagBhayani
  • 你用的是什么可拖拽的插件?
  • 请更新这个小提琴,以便我可以帮助你...jsfiddle.net/3eefs2kp
  • 我正在使用 jQuery ui 。好的,我会注册 jsfiddle 并告诉你

标签: javascript jquery jquery-ui jquery-ui-droppable


【解决方案1】:

看起来您正在尝试做的是两个连接的可排序,这已经由 sortable 小部件处理。你有几乎相同的事件,drop 几乎等同于receiveout 也可以。

目前还不完全清楚最终结果应该是什么,但这应该会给你一些想法:

    $(".sortable").sortable({
        connectWith: '.sortable',
        receive: function (event, ui) {
            // Set values
            var myid = ui.item.attr("id").toString();
            myid = myid.substring(0, myid.length - 1);
            document.getElementById(myid).value = myid;
        },
        out: function (event, ui) {
            // Unset the values
             console.log('out')
            var myid = ui.item.attr("id").toString();
            myid = myid.substring(0, myid.length - 1);
            document.getElementById(myid).value = myid;
        }
    });

http://jsfiddle.net/Lhn3zo0s/2/

【讨论】:

  • 你做的真的很棒,拖拽功能变得非常流畅。嗯......不完全是我想要的,但我从你的回答中得到了改变我最初方法的想法。我真的很感谢你的回答。谢谢
猜你喜欢
  • 2011-10-29
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多