2011-01-26 71 views
12

如何从购物车中删除商品?jquery draggable droppable remove dropped

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

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

    回答

    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/

    您可以将一个项目从车回目录中的任何一类,但我认为这将是很容易使项目仅可拖动到原来的类别。

    +0

    ... TY ...太多了!这将帮助很多人有同样的问题! – Spechal 2011-01-26 17:34:59