【问题标题】:jquery draggable droppable remove droppedjquery 可拖动 droppable 删除已删除
【发布时间】:2011-01-26 00:15:42
【问题描述】:

如何将商品从购物车中移除?

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

$(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" ); } }); });
  • 【问题讨论】:

      标签: javascript jquery jquery-ui user-interface


      【解决方案1】:

      这应该可行:

      $(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-items。它会调用 remove() 以在商品掉落后从购物车中移除商品。

      在这里查看它的工作原理:http://jsfiddle.net/andrewwhitaker/t97FE/embedded/result/

      您可以将商品从购物车拖回目录中的任何类别,但我认为让商品只能拖到其原始类别会很容易。

      【讨论】:

      • ... TY ... 这么多!这将帮助很多有同样问题的人!
      • 嗨 Andrew +1,如果我们想向购物车中的项目添加一个关闭按钮(看起来很接近的超链接)而不是拖放回来怎么办?我想在那种情况下我不需要catalog li.droppable 函数。如何使用您的代码来解决这个问题?
      • @aspiring:这是您可以做的一个示例:jsfiddle.net/t97FE/246。基本上,添加一个指向附加到购物车的li 的关闭链接。添加一个“点击”处理程序,当它被点击时移除父 li
      猜你喜欢
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 2013-06-22
      • 2018-05-13
      • 1970-01-01
      相关资源
      最近更新 更多