简体   繁体   English

带有可排序 UI 的 jQuery 拖放不起作用

[英]jQuery Drag and Drop with sortable UI not working

I'm currently trying to create a drag and drop, with sortable list items... But when I'm moving the draggable item towards the placeholder, it just returns to it's place.我目前正在尝试使用可排序的列表项创建拖放...但是当我将可拖动项移向占位符时,它只会返回到它的位置。 Any ideas what I'm doing wrong?任何想法我做错了什么?

I found this method here , but it doesn't seem to work for me.我在这里找到了这种方法,但它似乎对我不起作用。

Here is my HTML:这是我的 HTML:

<ul id="draggable">

    <li class="to-drag" class="items">
        <h1>1</h1>
        <p>Description 1</p>
    </li>

    <li class="to-drag" class="items">
        <h1>2</h1>
        <p>Description 2</p>
    </li>

    <li class="to-drag" class="items">
        <h1>3</h1>
        <p>Description 3</p>
    </li>

</ul>

<div style="height: 100px; clear: both;"></div>

<ul id="dropzone" class="items">

    <li class="placeholder"></li>

</ul>

And this is the jQuery:这是 jQuery:

$(function() {

    $("#draggable li.to-drag").draggable({

        connectWith: "#dropzone",
        helper: "clone",
        revert: "invalid",
        droppable: "drop"

    });

    $("#dropzone").droppable({

        drop: function( event, ui) {

            $(this).addClass("correct");

        }

    });

    $("ul, li").disableSelection();

});

Thanks for the help!谢谢您的帮助! :) :)

EDIT编辑

This is the working jQuery:这是工作的jQuery:

$(function() {

$("#dropzone").sortable({

    revert: true,
    opacity: 0.5

});

$("#draggable li").draggable({

    connectToSortable: ".items",
    helper: "clone",
    revert: true,
    opacity: 0.5

});

$("li.placeholder").droppable({
    revert: false,

    drop: function (event, ui) {

        var dragging = ui.draggable.clone();

        $(this).append(dragging);

    }

});

$("ul, li").disableSelection();

});

You've got at least three major problems:你至少有三个主要问题:

  • Draggable doesn't have connectWith , it has connectToSortable . Draggable没有connectWith ,它有connectToSortable So you should change this.所以你应该改变这个。 See doc: http://api.jqueryui.com/draggable/见文档: http : //api.jqueryui.com/draggable/

  • To use connectToSortable , you need a sortable , not at要使用connectToSortable ,您需要一个sortable ,而不是 at
    droppable . droppable

  • You'll need to change drop event for a sortable event, receive您需要更改可排序事件的drop事件, receive
    maybe or update , depending on what you need.也许或update ,取决于您的需要。
    http://api.jqueryui.com/sortable/ http://api.jqueryui.com/sortable/

It could look like this:它可能看起来像这样:

$(function() {

    $("#draggable li.to-drag").draggable({

        connectToSortable: "#dropzone",
        helper: "clone",
        revert: "invalid",
        droppable: "drop"

    });

    $("#dropzone").sortable({

        receive: function( event, ui) {

            $(this).addClass("correct");

        }

    });

    $("ul, li").disableSelection();

});

Please see the changes I have made请查看我所做的更改

    $(function() {

    $("#draggable li.to-drag").draggable({
    appendTo: "body",
      helper: "clone",
        revert:"invalid"
    });

    $("#dropzone1").droppable({

        drop:function( event, ui) {
alert(ui.draggable.text())
$("#dropzone").text( ui.draggable.text() ).appendTo( this );


        }

    });

    $("ul, li").disableSelection();

});

The additional div ensures you are dragging on to the right panel额外的 div 确保您拖动到右侧面板

    <ul id="draggable">

    <li class="to-drag" class="items">
        <h1>1</h1>
        <p>Description 1</p>
    </li>

    <li class="to-drag" class="items">
        <h1>2</h1>
        <p>Description 2</p>
    </li>

    <li class="to-drag" class="items">
        <h1>3</h1>
        <p>Description 3</p>
    </li>

</ul>

<div style="height: 100px; clear: both;"></div>
<div id="dropzone1" style="border:solid 1px">
    <ul id="dropzone" class="items" >

    <li class="placeholder"></li>

</ul>
</div>

Requires some tweaking to get some additional functionality but you should be able to do this需要一些调整以获得一些额外的功能,但你应该能够做到这一点

you will need additional code to do the sorting您将需要额外的代码来进行排序

Check out this link here Cheers Truez在此处查看此链接 Cheers Truez

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM