简体   繁体   English

jQuery对话框第二次不显示内容

[英]Jquery Dialog does not show content on second time

I have tried all the possible ideas to get rid of the problem, I know its a known issue, but how do I get past it, basically jQuery ui does not show the dialog when called twice, using 我已经尝试了所有可能的方法来解决该问题,我知道这是一个已知问题,但是如何克服这个问题,基本上,jQuery ui两次调用时不显示对话框,使用

self.$popup.dialog("open");

I am using jQuery Ui 1.8.9 and jQuery 1.4.4 我正在使用jQuery Ui 1.8.9和jQuery 1.4.4

Here's my code: 这是我的代码:

   self.$popup = $("#import_box_dialog").dialog({
        autoOpen: false,
        title: 'Import Albums',
        modal: true,
        position: "top",
        height: 600,
        maxWidth: 800 ,
        minWidth: 400 ,
        show:"slide",
        width :700,
        "buttons": [
            {
                text: "Import",
                click: self.doImport
            }
        ]
    });

Another queer thing i saw was that the dialog box content gets embedded in the outer DOM element. 我看到的另一件事是对话框内容嵌入在外部DOM元素中。

My dialog DOM structure is like : import_box-> 我的对话框DOM结构如下:import_box->

<div class="media_import_box" >
    <div id="import_box_dialog">
        <ul id="media_content"></ul>
    </div>
</div>

and when when it is displayed second time the DOM tree looks like this 当第二次显示时,DOM树如下所示

错误DOM树

The "import_box_dialog" actually moves out of the "import_box" div and I have no idea how it happens, but the display property is none, which does not change when i call the dialog again. 实际上,“ import_box_dialog”已移出“ import_box” div,我不知道它是如何发生的,但是display属性是none,当我再次调用对话框时,它不会改变。

$(document).ready(function() {
    var $dialog = $('<div></div>')
        .html('This dialog will show every time!')
        .dialog({
            autoOpen: false,
            title: 'Basic Dialog'
        });

    $('#opener').click(function() {
        $dialog.dialog('open');
        // prevent the default action, e.g., following a link
        return false;
    });
});

Basic usage of the jQuery UI dialog jQuery UI对话框的基本用法

Managed to solve the issue, the issue was that the program was repeatedly creating unnecessary div's whenever $("import_box_dialog").dialog() was called. 解决问题的方法是,只要$("import_box_dialog").dialog() ,程序就会反复创建不必要的div。 And since the $ operator returns u all the div's matching a given condition, ie ID in this case, hence the content did not become visible, which i think made it confused, there are two ways to solve it: 并且由于$运算符返回u所有与给定条件匹配的div,在这种情况下即ID,因此内容不可见,我认为这很令人困惑,有两种解决方法:

  • Remove the Div manually by calling $("import_box_dialog").child().remove() This would however remove all the other Div's inside the dialog div 通过调用$("import_box_dialog").child().remove()手动删除Div,但是这将删除对话框div中的所有其他Div。
  • Use a variable to track the div used for the dialog box, since I am using jquery classes, I use it for the static variable. 使用变量来跟踪用于对话框的div,因为我使用的是jquery类,所以将其用于静态变量。 Thus this way, it tracks the dialog, and always creates the dialog on the same div. 这样,它会跟踪对话框,并始终在同一div上创建对话框。

I suppose jquery applies a lot of class styles, due to which there is ambiguity in the selection of the proper div, and I guess using a static variable solves it. 我猜想jquery应用了很多类样式,由于它们在选择正确的div时存在歧义,我猜想使用静态变量可以解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM