jQuery UI 可拖动 & AJAX Post 到控制器操作 MVC.NET 后的可删除中断

发布于 2024-10-21 19:20:57 字数 986 浏览 1 评论 0原文

我在使此功能正常工作时遇到问题。

情况是:

  1. 用户将项目拖到 div 上。
  2. 在放置时,AJAX Post 会被发送到具有项目 ID 的控制器操作。
  3. 成功后,我用从数据库检索到的 html 标记替换该项目被拖放到的 div。 .replaceWith(data) 注意:此标记是一个 div,其 ID 与项目拖放到的 ID 相同,其中包含一些附加元素。
  4. 这会加载更新后的 div。

所有这些都完美地工作,几乎,问题是我无法将不同的项目拖到 div 上。我确信这与 AJAX Post 之后加载的新 div(具有相同 ID)有关。

这是我的 jQuery 代码

$(function () {
        $('.draggable').draggable({ revert: true });
        $('#layoutHeader').droppable({
            drop: function (event, ui) {
                $.ajax({
                    type: "POST",
                    url: '/SiteSetup/GetMarkup/' + $(ui.draggable).attr("id"),
                    success: function (data) {
                        $('.draggable').draggable('destroy');
                        $('#layoutHeader').replaceWith(data);
                        $('.draggable').draggable({ revert: true });

                    }
                });
            }
        });
    });

I'm having a problem getting this functionality to work.

Situation is:

  1. User Drags an item to a div
  2. On the drop an AJAX Post is sent to a controller action with the item ID
  3. On success I replace the div the item was dropped onto with html markup retrieved from the database. .replaceWith(data) Note: This markup is a div with the same ID as that which the item was dropped onto with some additional elements contained within.
  4. This loads the updated div.

All this works perfectly, well almost, the problem is that I cannot drag a different item onto the div. I'm sure it has something to do with the new div (with the same ID) being loaded in after the AJAX Post.

Here's my jQuery code

$(function () {
        $('.draggable').draggable({ revert: true });
        $('#layoutHeader').droppable({
            drop: function (event, ui) {
                $.ajax({
                    type: "POST",
                    url: '/SiteSetup/GetMarkup/' + $(ui.draggable).attr("id"),
                    success: function (data) {
                        $('.draggable').draggable('destroy');
                        $('#layoutHeader').replaceWith(data);
                        $('.draggable').draggable({ revert: true });

                    }
                });
            }
        });
    });

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

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

发布评论

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

评论(2

泪之魂 2024-10-28 19:20:57

使用 jQuery 和 DOM,如果您用完全相同的 DOM id 替换 DOM 元素(因此 id 为“div1”的 div 被另一个 id 为“div1”的 div 替换),则关联的 jQuery 数据将与该元素相关联。 DOM 元素丢失。 jQuery 的指针实际上指向页面上不再存在的幽灵元素。

要查看证据,请在 Firefox 中启动 firebug,然后简单地查看

$("#layoutHeader")

AJAX 帖子之前和之后。两次单击控制台中返回的元素。您会注意到,第二次单击(即 POST 返回后的一次)您的萤火虫将指向一个灰色的幽灵元素。

解决方案是稍微重构一下你的代码。试试这个

function handler(event,ui){
                $.ajax({
                    type: "POST",
                    url: '/SiteSetup/GetMarkup/' + $(ui.draggable).attr("id"),
                    success: function (data) {
                        $('.draggable').draggable('destroy');
                        $('#layoutHeader').replaceWith(data);
                        $('.draggable').draggable({ revert: true });

                    }
             $('#layoutHeader').droppable({drop: handler});
}


$(function () {
        $('.draggable').draggable({ revert: true });
        $('#layoutHeader').droppable({drop: handler});

    });

With jQuery and DOM, if you replace a DOM element with exactly the same DOM element with exactly the same DOM id ( so a div with id "div1" gets replaced by another div with id "div1") then the associated jQuery data with that DOM element gets lost. jQuery's pointer actually points to a ghost element which no longer exist on the page.

To see the proof, fire up firebug in Firefox and simply go

$("#layoutHeader")

before and after your AJAX post. Click on the element returned in the console both the times. You will notice that on the second click (i.e. one after the POST returns) your firebug will point to a greyed out ghost element.

Solution is to refactor your code a bit. Try this

function handler(event,ui){
                $.ajax({
                    type: "POST",
                    url: '/SiteSetup/GetMarkup/' + $(ui.draggable).attr("id"),
                    success: function (data) {
                        $('.draggable').draggable('destroy');
                        $('#layoutHeader').replaceWith(data);
                        $('.draggable').draggable({ revert: true });

                    }
             $('#layoutHeader').droppable({drop: handler});
}


$(function () {
        $('.draggable').draggable({ revert: true });
        $('#layoutHeader').droppable({drop: handler});

    });
彡翼 2024-10-28 19:20:57

保留要插入 DOM 的 HTML 的 id 不会保留附加到您的 divdroppable 小部件正在更换。

您需要重组代码,以便在新内容上重新初始化 droppable 小部件:

$('.draggable').draggable({ revert: true });

function createDroppable() {
    $('#layoutHeader').droppable({
        drop: function (event, ui) {
            $.ajax({
                type: "POST",
                url: '/SiteSetup/GetMarkup/' + $(ui.draggable).attr("id"),
                success: function (data) {
                    $('.draggable').draggable('destroy');
                    $('#layoutHeader').replaceWith(data);

                    /* Re-initialize the droppable widget: */
                    createDroppable();

                    $('.draggable').draggable({ revert: true });
                }
            });
        }
    });
}   

createDroppable();

或者,您可以用另一个 div< 包装 #layoutHeader /code> 充当可放置。这样,替换 #layoutHeader 不应删除 droppable 功能。

Keeping the id of the HTML you're inserting into the DOM isn't going to preserve the droppable widget that was attached to the div you're replacing.

You'll need to restructure your code so that the droppable widget is re-initialized on the new content:

$('.draggable').draggable({ revert: true });

function createDroppable() {
    $('#layoutHeader').droppable({
        drop: function (event, ui) {
            $.ajax({
                type: "POST",
                url: '/SiteSetup/GetMarkup/' + $(ui.draggable).attr("id"),
                success: function (data) {
                    $('.draggable').draggable('destroy');
                    $('#layoutHeader').replaceWith(data);

                    /* Re-initialize the droppable widget: */
                    createDroppable();

                    $('.draggable').draggable({ revert: true });
                }
            });
        }
    });
}   

createDroppable();

Alternatively, you could wrap #layoutHeader with another div that acts as the droppable. That way, replacing #layoutHeader shouldn't remove the droppable functionality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文