gpt4 book ai didi

javascript - 购物车 - 拖放错误

转载 作者:行者123 更新时间:2023-11-28 01:46:16 29 4
gpt4 key购买 nike

我已经根据示例实现了jQuery的购物卡:http://jsfiddle.net/RR6z5/1/

但是有一个小错误。当我将一个元素从产品拖到购物卡,然后无拖放,我从购物卡拖到产品时,产品中的原始元素消失了。如何避免这种情况?

谢谢。

$(function () {
$("#catalog").accordion();
$("#catalog li").draggable({
appendTo: "body",
helper: "clone"
});
$("#cart ol").droppable({
out: function (event, ui) {
var self = ui;
ui.helper.off('mouseup').on('mouseup', function () {
$(this).remove();
self.draggable.remove();
});
},
activeClass: "ui-state-default",
hoverClass: "ui-state-hover",
accept: ":not(.ui-sortable-helper)",
drop: function (event, ui) {
if (ui.draggable.is('.dropped')) return false;
$(this).find(".placeholder").remove();
$("<li></li>").text(ui.draggable.text()).appendTo(this).draggable({
appendTo: "body",
helper: "clone"
}).addClass('dropped');
}
}).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");
}
});


});

最佳答案

请注意,我修改了一些内容:

if(self.draggable.parents('#cart').length){
self.draggable.remove();
}

查询确保元素被拖动到#cart元素中,然后将其删除。

如果拖动的元素是原始元素(必须不在#cart元素中,而是在#products元素中),则该元素不会被删除。

$(function () {
$("#catalog").accordion();
$("#catalog li").draggable({
appendTo: "body",
helper: "clone"
});
$("#cart ol").droppable({
out: function (event, ui) {
var self = ui;
ui.helper.off('mouseup').on('mouseup', function () {
$(this).remove();
if(self.draggable.parents('#cart').length){
self.draggable.remove();
}
});
},
activeClass: "ui-state-default",
hoverClass: "ui-state-hover",
accept: ":not(.ui-sortable-helper)",
drop: function (event, ui) {
if (ui.draggable.is('.dropped')) return false;
$(this).find(".placeholder").remove();
$("<li></li>").text(ui.draggable.text()).appendTo(this).draggable({
appendTo: "body",
helper: "clone"
}).addClass('dropped');
}
}).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");
}
});


});

您可以在 http://jsfiddle.net/RR6z5/30/ 上查看

关于javascript - 购物车 - 拖放错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20286250/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com