向加载了AJAX的内容添加JQuery

时间:2021-09-25 19:43:47

I just learned that you can use live() instead of bind() to make sure an event handler still exists after content is reloaded with AJAX... but what can I do with other added JQuery, such as .sortable()? I have a UL that I need to reload after sorting or adding; but after the AJAX reload, the sorting doesn't quite work right...

我刚刚了解到,您可以使用live()而不是bind()来确保在用AJAX重新加载内容之后,事件处理程序仍然存在……但是对于其他添加的JQuery,比如.sortable(),我能做什么呢?我有一个UL,我需要在排序或添加后重新加载;但是在AJAX重新加载之后,排序就不太正常了……

It doesn't completely lose the sortability, but the dragging become much more difficult; it will jump around and seemingly switch 2 other sections that's not the one I'm trying to drag. This only happens after the partial reload; my guess is that when it reloads, I need to re-attach the sortable... how can I do this? Here's my code:

它不会完全失去吸附能力,但是拖拽会变得更加困难;它会跳来跳去,好像切换到另外两个部分,而不是我想要拖动的那部分。这只发生在部分重载之后;我的猜测是,当它重新加载时,我需要重新加载可排序的……我该怎么做呢?这是我的代码:

$("#sections").sortable({
                start: function (event, ui) {
                    startIndex = ui.item.index();
                },
                update: function (event, ui) {
                    $.ajax({
                        type: "POST",
                        url: "/Form/sortSections",
                        data: {
                            formID: '@(Model.formID)',
                            displayOrder: ui.item.index() + 1,
                            sectionID: $(ui.item).attr("id"),
                            startDisplayOrder: startIndex + 1
                        },
                        success: function (result) {
                            $.get("/Form/_AllSections/@(Model.formID)", function (data) {
                                $("#sections").html(data);
                            });
                        },
                        error: function (req, status, error) {
                            alert(error);
                        }
                    });
                }
            });

1 个解决方案

#1


3  

Use the load event with live() so that it runs every time the element is added to the DOM.

使用live()中的load事件,以便在每次将元素添加到DOM时运行它。

http://api.jquery.com/load-event

http://api.jquery.com/load-event

#1


3  

Use the load event with live() so that it runs every time the element is added to the DOM.

使用live()中的load事件,以便在每次将元素添加到DOM时运行它。

http://api.jquery.com/load-event

http://api.jquery.com/load-event