使用ajax拖放后拖动的项目消失

发布于 2024-11-08 18:52:22 字数 1696 浏览 5 评论 0原文

我确实对 jQuery 拖放有疑问。让我稍微解释一下情况。

我想要实现的目标如下:当您在时间轴上拖动一个泪滴时,div 中的 id 以及它所放置的项目的 id 应该在放置后立即存储在我的数据库中。

我设法用 ajax 做到这一点,代码如下:

$('#dropzone ul li').droppable({
  drop: function(event, ui) {
        var day = $(".confirmday").val();
        var $drag = $(ui.draggable),
        var $drop = $(this);

        var drag = $drag.attr('id');
        var drop = $drop.attr('id');

        console.log(drag);

        if(drag == 'drag1')
        {
            drag = 'Medicijnen';
            werkwoord = ' ingenomen om ';
        }
        else if(drag == 'drag2')
        {
            drag = 'Snack';
            werkwoord = ' gegeten om ';
        }
        else if(drag == 'drag3')
        {
            drag = 'Frisdrank';
            werkwoord = ' gedronken om ';
        }
        else if(drag == 'drag4')
        {
            drag = 'Thee';
            werkwoord = ' gedronken om ';
        }
        else if(drag == 'drag5')
        {
            drag = 'Koffie';
            werkwoord = ' gedronken om ';
        }
        else if(drag == 'drag6')
        {
            drag = 'Alcohol';
            werkwoord = ' gedronken om ';
        }

        $.post("ajax/save.php", { 
                drag: drag,
                drop: drop,
                userid: <?php echo $_SESSION['id']; ?>,
                day: day},
                function(data) 
                {
                });
        return false;
    },
});

但是当我这样做时,拖动的项目(或该项目的克隆)就会消失。不知道为什么,页面没有刷新。新物品无法存储。

我尝试的另一个选项是将拖动的项目的 ID 和放置的项目 ID 放入文本框中,并在每次放置某些东西时保存它。但这也不会起作用。

我真的希望有人能帮助我! 提前致谢。

在此处输入图像描述

I really have problems with the jQuery drag and drop. So let me explain the situation a bit.

What I want to achieve is the following: when you drag one of the teardrops on the timeline, the id from the div and the id from the item it's dropped on, should be stored in my db immediately after the drop.

I managed to do that with ajax, here's the code:

$('#dropzone ul li').droppable({
  drop: function(event, ui) {
        var day = $(".confirmday").val();
        var $drag = $(ui.draggable),
        var $drop = $(this);

        var drag = $drag.attr('id');
        var drop = $drop.attr('id');

        console.log(drag);

        if(drag == 'drag1')
        {
            drag = 'Medicijnen';
            werkwoord = ' ingenomen om ';
        }
        else if(drag == 'drag2')
        {
            drag = 'Snack';
            werkwoord = ' gegeten om ';
        }
        else if(drag == 'drag3')
        {
            drag = 'Frisdrank';
            werkwoord = ' gedronken om ';
        }
        else if(drag == 'drag4')
        {
            drag = 'Thee';
            werkwoord = ' gedronken om ';
        }
        else if(drag == 'drag5')
        {
            drag = 'Koffie';
            werkwoord = ' gedronken om ';
        }
        else if(drag == 'drag6')
        {
            drag = 'Alcohol';
            werkwoord = ' gedronken om ';
        }

        $.post("ajax/save.php", { 
                drag: drag,
                drop: drop,
                userid: <?php echo $_SESSION['id']; ?>,
                day: day},
                function(data) 
                {
                });
        return false;
    },
});

But when I do that, the dragged item (or the clone of that item) just disappears. I don't know why, because the page doesn't refresh. The new item just won't store.

The other option I tried was to put the dragged item's ID and the dropped on item ID in a text box and to save that every time something is dropped. But that won't work as well.

I really hope someone can help me!
Thanks in advance.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2024-11-15 18:52:22

您将在 drop 回调处理程序末尾返回 false

这将告诉 droppable 该draggable 无效,然后将其丢弃。如果你完全删除 return ,它应该可以工作。

我冒昧地优化了您的代码,希望您不要介意;)

$('#dropzone ul li').droppable({
    drop: function(event, ui) {
        var day = $(".confirmday").val(),
            drag, werkwoord;

        switch (ui.draggable[0].id) {
            case "drag1":
                drag = 'Medicijnen';
                werkwoord = ' ingenomen om ';
            break;

            case "drag2":
                drag = 'Snack';
                werkwoord = ' gegeten om ';
            break;

            case "drag3":
                drag = 'Frisdrank';
                werkwoord = ' gedronken om ';
            break;

            case "drag4":
                drag = 'Thee';
                werkwoord = ' gedronken om ';
            break;

            case "drag5":
                drag = 'Koffie';
                werkwoord = ' gedronken om ';
            break;

            case "drag6":
                drag = 'Alcohol';
                werkwoord = ' gedronken om ';
            break;

            default:
                return false;
            break;
        }

        $.post("ajax/save.php",
            { 
                drag: ui.draggable[0].id,
                drop: this.id,
                userid: <?php echo $_SESSION['id']; ?>,
                day: day
            },
            function(data){}
        );
    },
});

You are returning false at the end of the drop callback handler.

This will tell the droppable that the draggable was invalid and will then discard it. If you remove return altogether it should work.

I took the liberty to optimize your code, I hope you don't mind ;)

$('#dropzone ul li').droppable({
    drop: function(event, ui) {
        var day = $(".confirmday").val(),
            drag, werkwoord;

        switch (ui.draggable[0].id) {
            case "drag1":
                drag = 'Medicijnen';
                werkwoord = ' ingenomen om ';
            break;

            case "drag2":
                drag = 'Snack';
                werkwoord = ' gegeten om ';
            break;

            case "drag3":
                drag = 'Frisdrank';
                werkwoord = ' gedronken om ';
            break;

            case "drag4":
                drag = 'Thee';
                werkwoord = ' gedronken om ';
            break;

            case "drag5":
                drag = 'Koffie';
                werkwoord = ' gedronken om ';
            break;

            case "drag6":
                drag = 'Alcohol';
                werkwoord = ' gedronken om ';
            break;

            default:
                return false;
            break;
        }

        $.post("ajax/save.php",
            { 
                drag: ui.draggable[0].id,
                drop: this.id,
                userid: <?php echo $_SESSION['id']; ?>,
                day: day
            },
            function(data){}
        );
    },
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文