【问题标题】:jquery ui dialog box removes <div> after closingjquery ui对话框关闭后删除<div>
【发布时间】:2011-07-27 18:27:25
【问题描述】:

我有三个 div,每个都有一个按钮,用于触发我的对话框。当我触发父 div 时,它会显示对话框和其他两个孩子的内容,没有问题。

如果只触发子div,则打开并显示子div的内容就ok了,

但是当我打开[单击对话框按钮]父div时,子div内容消失[通过firebug检查]或不显示,

但是当我触发子 div 的按钮时,对话框打开并且内容正常。

请帮我解决我所缺少的。谢谢。

        <div id="parentDialog">
         <div id="childoneDialog"></div>
         <div id="childtwoDialog"></div>
        </div>
    <input id="buttonParent" type="button" />
    <input id="buttonChildone" type="button" />

$("#buttonParent").click( $("parentDialog").dialog.("open") .... );
$("#buttonChildone").click( $("childoneDialog").dialog.("open") .... );

【问题讨论】:

    标签: jquery jquery-ui dialog


    【解决方案1】:

    这里的问题是你的对话框嵌套了。 jQuery 对话框的工作方式是它假定所有对话框都必须是独占的。不要嵌套你的对话框,你应该没问题。

    更新:根据您在下面的评论,您可以尝试以下方法。它并不完美,但它应该可以完成这项工作。例如:

    <div id="Dialog1">
        <!-- dialog1 stuff here -->
        <div class="formTarget"><!-- dynamically insert form here --></div>
        <!-- more dialog1 stuff here -->
    </div>
    
    <div id="Dialog2">
        <!-- dialog2 stuff here -->
        <div class="formTarget"><!-- dynamically insert form here --></div>
        <!-- more dialog2 stuff here -->
    </div>
    
    <div id="reusableForm">
        <form><!--yourformhere--></form>
    </div>
    
    <script>
    function triggerDialog(d) { // note that 'd' is the ID of the dialog you want to trigger
       $('#reusableForm').appendTo('#'+d+' .formTarget');
       $(''#'+d).dialog('open');
    }
    </script>
    

    在您想要触发弹出窗口的任何链接或按钮上,执行以下操作:

    <a href="#" onclick="triggerDialog('Dialog1')">Click here to trigger Dialog 1</a>
    <button type="button" onclick="triggerDialog('Dialog2')">Click here to trigger Dialog 2</button>
    

    实质上,此代码将表单移动到一个对话框或另一个对话框中,具体取决于传递给 triggerDialog 函数的内容。它并不完美,但它应该让您对 jQuery 和 DOM 操作的选项有所了解。您应该查看的其他一些 jQuery 方法是:

    $.append()
    $.clone()
    $.after() / $.before()
    

    查看http://api.jquery.com/ 了解更多文档。

    【讨论】:

    • 我明白了。如果您提供更多关于您的目标是什么的详细信息将会很有帮助 - 您试图达到的结果是什么。我根据您的反馈更新了我的答案...
    • 谢谢你,乔希,我已经做了你的代码,在你评论对话框不会嵌套之后。我需要的是,
      # .....
      # .....
      在添加新条目时,我需要两个孩子,但在编辑条目时,我将分别需要它们。谢谢你的更新,我会尝试更新我的代码,唷,傻我还没想到追加,thnks2
    猜你喜欢
    • 2011-01-16
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 2012-02-25
    • 2010-10-28
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    相关资源
    最近更新 更多