jQuery UI 对话框 - 更改打开的对话框的内容 (Ajax)

发布于 2024-09-12 01:30:32 字数 2663 浏览 4 评论 0原文

我想使用 jQuery.load() 在 jQuery UI 对话框中动态打开一些链接。对话框打开后,我希望链接加载到已打开的对话框中。

  1. 因此,该网站加载后,您单击一个链接,它会在一个对话框中打开。没关系。您可以根据需要多次关闭和打开它。
  2. 当它打开时,如果您单击加载内容中的链接之一,它将不起作用。
    • 执行了 Ajax GET 请求,但生成的内容未执行成功加载到对话框中。 (Firebug 显示请求)
    • 先前的对话框标题和对话框内容将被删除。但新内容没有显示,也没有插入到 DOM 中。 (生成的源不会在任何地方显示内容。)

链接如下所示...

<a href="http://www.example.com/index.php?action=something&amp;search=somethingelse#fragment" rel="dialog" title="Title Attribute">

单击事件已绑定...

$('body').delegate("a[rel~=dialog]", "click", function(e){return ajax_dialog(this, e);});

ajax_dialog 函数检查是否存在对话框,如果存在则调用创建一个对话框没有,调用加载内容,设置标题,并打开对话框(如果未打开)。

function ajax_dialog(_this, _event){
    var urlToLoad = $(_this).attr("href").replace("#", "&ajax=true #");
    var linkTitle = $(_this).attr("title");

    // Create dialog
    if(!$('body').find('#ajaxDialog').size()){
        $('body').append('not yet init<br />'); // This shows up the first click only.
        init_dialog('#ajaxDialog');
    }

    // Load Dialog Content
    load_dialog('#ajaxDialog', urlToLoad);

    // Add title
    $('#ajaxDialog').dialog('option', 'title', linkTitle);

    // Open dialog (or reload)
    if(!$('#ajaxDialog').dialog('isOpen')){
        $('#ajaxDialog').dialog('open');
        $('body').append('not yet open<br />'); // This shows up the first click only.
    }
    return false;
}

如果没有对话框,init_dialog 函数将创建对话框...

function init_dialog(_this){
    $('body').append('<div id="ajaxDialog"></div>');
    // Set Dialog Options
    $(_this).dialog({
        modal:true,
        autoOpen:false,
        width:900,
        height:400,
        position:['center','center'],
        zIndex: 9999,
        //open:function(){load_dialog(this, urlToLoad);}, This didn't work without destroying the dialog for each click.
        close:function(){$(this).empty();}
    });
}

load_dialog 函数将所需的内容加载到对话框中。

function load_dialog(_this, urlToLoad){
    $(_this).load(urlToLoad, function(){
        $('body').append(urlToLoad + ' load function<br />'); // This shows up each click
        $(_this).append("Hihi?"); // This shows up each click
    });
    // The loaded information only shows the first click, other times show an empty dialog.
}

I've got some links I want to have dynamically open in a jQuery UI Dialog using jQuery.load(). Once the dialog is open, I want the links load inside the already opened dialog.

  1. So, the site loads, you click a link, and it opens in a dialog. That's fine. You can close and open it as many times as you want.
  2. While it's open, if you click on one of the links from the loaded content, it doesn't work.
    • An Ajax GET request IS performed, but the resulting content is not successfully loaded into the dialog. (Firebug shows the request)
    • The previous dialog title and dialog content is erased. But the new content is not shown, NOR is it inserted into the DOM. (The generated source does not show content anywhere.)

The links look like this...

<a href="http://www.example.com/index.php?action=something&search=somethingelse#fragment" rel="dialog" title="Title Attribute">

The click event is bound...

$('body').delegate("a[rel~=dialog]", "click", function(e){return ajax_dialog(this, e);});

The ajax_dialog function checks to see if there's a dialog, calls to create one if there isn't, calls to load the content, sets the title, and opens the dialog if it's not open.

function ajax_dialog(_this, _event){
    var urlToLoad = $(_this).attr("href").replace("#", "&ajax=true #");
    var linkTitle = $(_this).attr("title");

    // Create dialog
    if(!$('body').find('#ajaxDialog').size()){
        $('body').append('not yet init<br />'); // This shows up the first click only.
        init_dialog('#ajaxDialog');
    }

    // Load Dialog Content
    load_dialog('#ajaxDialog', urlToLoad);

    // Add title
    $('#ajaxDialog').dialog('option', 'title', linkTitle);

    // Open dialog (or reload)
    if(!$('#ajaxDialog').dialog('isOpen')){
        $('#ajaxDialog').dialog('open');
        $('body').append('not yet open<br />'); // This shows up the first click only.
    }
    return false;
}

The init_dialog function creates the dialog if there isn't one...

function init_dialog(_this){
    $('body').append('<div id="ajaxDialog"></div>');
    // Set Dialog Options
    $(_this).dialog({
        modal:true,
        autoOpen:false,
        width:900,
        height:400,
        position:['center','center'],
        zIndex: 9999,
        //open:function(){load_dialog(this, urlToLoad);}, This didn't work without destroying the dialog for each click.
        close:function(){$(this).empty();}
    });
}

The load_dialog function loads the desired content into the dialog.

function load_dialog(_this, urlToLoad){
    $(_this).load(urlToLoad, function(){
        $('body').append(urlToLoad + ' load function<br />'); // This shows up each click
        $(_this).append("Hihi?"); // This shows up each click
    });
    // The loaded information only shows the first click, other times show an empty dialog.
}

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

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

发布评论

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

评论(1

白馒头 2024-09-19 01:30:32

哈。如代码所示,我使用 $jQuery.load() 并将链接的确切 href 拉为 URL 请求。所有 URL 末尾都有片段/锚点,即: ....html#id-of-div

在本例中,脚本本身工作正常,但页面上尚不存在#id-of-div。这就是为什么我可以看到返回的内容,但对话框最终变成空白。 :-)

Hah. As shown in the code, I was using $jQuery.load() and pulling the exact href of the link as the URL to request. All the URLs had fragments/anchors on the end, that is: ....html#id-of-div.

In this case, the script itself was working fine, but the #id-of-div didn't exist on the page yet. That's why I could see content returned, but the dialog just ended up blank. :-)

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