最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery draggable droppable remove dropped - Stack Overflow

programmeradmin2浏览0评论

How would one remove the item from the shopping cart?

Naturally you would want to be able to drag and drop the item back.

$(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" ); } }); });

    How would one remove the item from the shopping cart?

    Naturally you would want to be able to drag and drop the item back.

    $(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" ); } }); });
    Share Improve this question edited Jan 13, 2015 at 15:48 Taryn 248k57 gold badges370 silver badges408 bronze badges asked Jan 26, 2011 at 0:15 SpechalSpechal 2,7166 gold badges34 silver badges50 bronze badges
    Add a comment  | 

    1 Answer 1

    Reset to default 16

    This should work:

    $(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'
        });
    });
    

    Notes:

    • When an item is dropped on the cart area, I've added a class of cart-item to the new item.
    • I've made the catalog's ul droppable; this area only accepts cart-items. It calls remove() to remove an item from the cart once the drop has occurred.

    See it working here: http://jsfiddle.net/andrewwhitaker/t97FE/embedded/result/

    You can drag an item back from the cart to any category in the catalog, but I think it would be pretty easy to make items only draggable to their original categories.

    发布评论

    评论列表(0)

    1. 暂无评论