带有 AJAX 选项卡的 jQuery UI 对话框问题

发布于 2024-08-19 02:00:21 字数 1202 浏览 11 评论 0原文

我在使用 jQuery 工具选项卡 (AJAX) 和 jQuery UI 对话框(手动为对话框执行 AJAX 加载)时遇到问题。问题在于该对话框是专门针对当前选项卡加载和设置的(通过 AJAX 在选项卡请求上加载,以及选项卡的内容)。 jQuery UI 在创建对话框时,将容器从其原始位置删除,添加其标记,并将其附加到主体。

问题是该对话框现在位于选项卡内容之外,并且在后续选项卡更改时不会被替换/删除。如果用户单击另一个选项卡或后退按钮(这些选项卡具有使用 URL 哈希的 AJAX 历史记录,因此页面实际上并未重新加载),该对话框将被破坏,但不会被删除,因为 jQuery UI 对话框将其移出选项卡内容。它现在只出现在身体的底部。如果用户实际单击关闭按钮,我为此编写了一个“hack around”,但是如果按下后退按钮或通过 AJAX 加载另一个选项卡,则不会触发此操作,并且 jQuery UI 实际上会将其移回底部身体(不知道它是如何/为什么这样做的!)。有什么建议吗?如果我对此不清楚,请告诉我。谢谢! (这是我现在得到的,当用户物理关闭对话框时成功删除它)

    $('.openMyDialog').click(function() {

                    // Create div for dialog
        $('body').append('<div id="modalContainer"></div>');

        // Load dialog with AJAX
        $('#modalContainer').load('loadMyAjaxContent.html').dialog({
            modal: true,
            width: 850,
            open: function(type,data) {
                // Remove from bottom of body and put it back into the tab's content
                $(this).parent().appendTo('.panes div:first');
            },
            close: function() {
                $(this).dialog('destroy');
                $('#modalContainer').remove();
            }
        });


    });

I'm having an issue using jQuery Tools Tabs (AJAX) along with jQuery UI Dialog (manually doing the AJAX loading for the dialog). The issue is that the dialog is loaded and setup specifically for the current tab (loaded on the tab request via AJAX, along with the tab's content). jQuery UI, when creating the dialog, removes the container from it's original position, adds its markup, and appends it to the body.

The issue is that the dialog is now outside of the tab's content and will not get replaced/removed on subsequent tab changes. If the user clicks another tab, or the back button (these tabs have an AJAX history using the URL hash, so the page is not actually reloaded), the dialog is broken but doesn't get removed since jQuery UI Dialog moved it outside of the tab content. It now just shows up at the bottom of the body. I wrote a 'hack around' for this if the user physically clicks the close button, but this doesn't get fired if the back button is pressed or another tab is loaded via AJAX and jQuery UI actually MOVES IT BACK to the bottom of the body (Not sure how/why it does that!). Any suggestions? And please let me know if I wasn't clear on any of that. Thanks! (Here's what I've got now, that successfully removes it when the users physically closes the dialog)

    $('.openMyDialog').click(function() {

                    // Create div for dialog
        $('body').append('<div id="modalContainer"></div>');

        // Load dialog with AJAX
        $('#modalContainer').load('loadMyAjaxContent.html').dialog({
            modal: true,
            width: 850,
            open: function(type,data) {
                // Remove from bottom of body and put it back into the tab's content
                $(this).parent().appendTo('.panes div:first');
            },
            close: function() {
                $(this).dialog('destroy');
                $('#modalContainer').remove();
            }
        });


    });

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

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

发布评论

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

评论(1

梦里梦着梦中梦 2024-08-26 02:00:21

假设设置有一个带有一堆 jQuery Ajax 选项卡的主页,并且每个选项卡 ajax 将一个页面加载到页面内容面板/div 中。

page.html

 ----/home\---/users\---
|<div id="page-content">|
|   page content gets   |
|   loaded in here      |
|</div>                 |

在通过选项卡获取 ajax 的页面中,将其放在顶部:

即。在 users.html 中

$(document).ready(function()
{
    $('.delete-user-form').each(function(){
        if($('.delete-user-form').length > 1)
            $(this).detach();
    });

    $('#delete-user').dialog({
        autoOpen: false,
        height: 200,
        width: 300,
        modal: true,
        buttons: {
            Cancel: function() {
                $(this).dialog('close');
            },
            'Save': function() {
                $('#delete-user-form').submit();
                $(this).dialog('close');
            }
        }
    });

    $('#delete-user-button').click(function(){
        $('#delete-user').dialog('open');
    });
});

这就是 users.html 中准备好的表单,它将变成对话框:

<div id="delete-user" class="delete-user-form" title="Delete User" style="display:none;">
    <form action="/admin/users/delete" id="delete-user-form" method="POST">
        <input type="hidden" id="user-id" name="userID" />
        <table cellspacing="0" cellpadding="5px" border="0" id="delete-user-form-table" style="display:none;">
            <tr>
                <td>
                    Do you really want to delete:
                </td>
                <td>
                    <input type="text" id="user-name" />
                </td>
            </tr>
        </table>
    </form>
</div>

现在正如OP所描述的,每次用户在选项卡之间切换并返回到此页面时,模式对话框 div (id="删除用户”)被重复。

javascript 的第一部分会迭代所有重复的对话框,并从 DOM 中删除除最后一个对话框之外的所有对话框,这样您最终将只得到一个所需的对话框。

在页面上放置一个按钮/链接/等,其 ID 与顶部位中指定的 ID 相同,以打开对话框。

IE。

这种方式打开对话框可以防止OP在省略时发现重复:autoOpen : 错误的,
从对话框设置选项中直接调用对话框。

Assuming the setup of having a main page with a bunch of jQuery Ajax tabs and each tab ajax-loads a page into the page content-panel/div.

page.html

 ----/home\---/users\---
|<div id="page-content">|
|   page content gets   |
|   loaded in here      |
|</div>                 |

Within the page that gets ajax'd in by the tab, put this at the top:

ie. in users.html

$(document).ready(function()
{
    $('.delete-user-form').each(function(){
        if($('.delete-user-form').length > 1)
            $(this).detach();
    });

    $('#delete-user').dialog({
        autoOpen: false,
        height: 200,
        width: 300,
        modal: true,
        buttons: {
            Cancel: function() {
                $(this).dialog('close');
            },
            'Save': function() {
                $('#delete-user-form').submit();
                $(this).dialog('close');
            }
        }
    });

    $('#delete-user-button').click(function(){
        $('#delete-user').dialog('open');
    });
});

And that's the prepared form in users.html which will be turned into the dialog box:

<div id="delete-user" class="delete-user-form" title="Delete User" style="display:none;">
    <form action="/admin/users/delete" id="delete-user-form" method="POST">
        <input type="hidden" id="user-id" name="userID" />
        <table cellspacing="0" cellpadding="5px" border="0" id="delete-user-form-table" style="display:none;">
            <tr>
                <td>
                    Do you really want to delete:
                </td>
                <td>
                    <input type="text" id="user-name" />
                </td>
            </tr>
        </table>
    </form>
</div>

Now as the OP describes, every time the user switches between tabs and comes back to this page, the modal dialog div (id="delete-user") gets duplicated.

The first bit of javascript iterates over all the duplicate dialogs and deletes all but the last one from the DOM so you'll end up with only one as desired.

Place a button/link/etc on the page with the ID as specified in the top bit to open the dialog.

ie. <input type="button" id="delete-user-button" value="Delete User" />

Opening the dialog this way prevents the duplication that the OP found when he omitted: autoOpen: false,
from the dialog setup options and called the dialog directly.

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