jquery draggable droppable删除掉了

时间:2011-01-26 00:15:43

标签: javascript jquery jquery-ui user-interface

如何从购物车中删除该项目?

当然,您希望能够将项目拖放回来。

$(function() {
        $( "#catalog" ).accordion();
        $( "#catalog li" ).draggable({
            appendTo: "body",
            helper: "clone"
        });
        $( "#cart ol" ).droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: ":not(.ui-sortable-helper)",
            drop: function( event, ui ) {
                $( this ).find( ".placeholder" ).remove();
                $( "
  • " ).text( ui.draggable.text() ).appendTo( this ); } }).sortable({ items: "li:not(.placeholder)", sort: function() { // gets added unintentionally by droppable interacting with sortable // using connectWithSortable fixes this, but doesn't allow you to customize active/hoverClass options $( this ).removeClass( "ui-state-default" ); } }); });

    1 个答案:

    答案 0 :(得分:16)

    这应该有效:

    $(function() {
        $("#catalog").accordion();
        $("#catalog li").draggable({
            appendTo: "body",
            helper: "clone"
        });
        $("#cart ol").droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: ":not(.ui-sortable-helper)",
            drop: function(event, ui) {
                $(this).find(".placeholder").remove();
                $("<li></li>").text(ui.draggable.text())
                    .addClass("cart-item")
                    .appendTo(this);
            }
        }).sortable({
            items: "li:not(.placeholder)",
            sort: function() {
                $(this).removeClass("ui-state-default");
            }
        });
        $("#catalog ul").droppable({
            drop: function(event, ui) {
                $(ui.draggable).remove();
            },
            hoverClass: "ui-state-hover",
            accept: '.cart-item'
        });
    });
    

    备注

    • 当商品被放置在购物车区域时,我在新商品中添加了cart-item类。
    • 我已将目录的ul删除了;此区域仅接受cart-item s。发生丢弃后,它会调用remove()从购物车中删除商品。

    在此处查看:http://jsfiddle.net/andrewwhitaker/t97FE/embedded/result/

    您可以将商品从购物车拖回目录中的任何类别,但我认为制作只能拖动到原始类别的商品非常容易。