如何在对话框中关闭 jQuery Dialog?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2933826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:35:04  来源:igfitidea点击:

How to close jQuery Dialog within the dialog?

jqueryjquery-uijquery-ui-dialog

提问by imperialx

How to close jQuery Dialog within the dialog without using the close button?

如何在不使用关闭按钮的情况下关闭对话框中的 jQuery Dialog?

Inside the Dialog is a simple form request. If a successful submission occurs, then the UI dialog automatically closes and refreshes the parent page:

Dialog 里面是一个简单的表单请求。如果提交成功,则 UI 对话框会自动关闭并刷新父页面:

<script type="text/javascript">
    $(document).ready(function () {
        $("#form-dialog").dialog({
            autoOpen: true,
            modal: true,
            width: 200,
            draggable: true,
            resizable: true
        });
    });
</script>

<div id="form-dialog" title="Form Submit">
    <form action="default.aspx" method="post">
        <input type="text" name="name" value=" " />    
        <input type="submit" value="submit" />

        <a href="#" id="btnDone">CLOSE</a>

        <script type="text/javascript">
        $(document).ready(function () {
            $("#btnDone").click(function () {
                $(this).dialog('close');
            });
        });
        </script>
    </form>
</div>

回答by Kunal

Try This

尝试这个

$(this).closest('.ui-dialog-content').dialog('close'); 

It will close the dialog inside it.

它将关闭其中的对话框。

回答by Jason

you can close it programmatically by calling

您可以通过调用以编程方式关闭它

$('#form-dialog').dialog('close')

whenever you want.

无论你什么时候想要。

回答by redsquare

You need

你需要

$('selector').dialog('close');

回答by Der_Meister

Close from iframe inside a dialog:

从对话框内的 iframe 关闭:

window.parent.$('.ui-dialog-content:visible').dialog('close');

回答by ndrix

$(this).parents(".ui-dialog-content").dialog('close')

Simple, I like to make sure I don't:

很简单,我想确保我不会:

  1. hardcode dialog #id values.
  2. Close all dialogs.
  1. 硬编码对话框#id 值。
  2. 关闭所有对话框。

回答by Marco

After checking all of these answers above without luck, the folling code worked for me to solve the problem:

在没有运气的情况下检查了上面的所有这些答案后,下面的代码为我解决了这个问题:

$(".ui-dialog").dialog("close");

Maybe this will be also a good try if you seek for alternatives.

如果您正在寻找替代品,也许这也是一个很好的尝试。

回答by Bart

replace one string to

将一个字符串替换为

$("#form-dialog").dialog('close');

$(this) here means another object $("#btnDone")

$(this) 在这里表示另一个对象 $("#btnDone")

 <script type="text/javascript">
    $(document).ready(function () {
        $("#form-dialog").dialog({
            autoOpen: true,
            modal: true,
            width: 200,
            draggable: true,
            resizable: true
        });
    });
</script>


<div id="form-dialog" title="Form Submit">
<form action="default.aspx" method="post">
<input type="text" name="name" value=" " />    
<input type="submit" value="submit" />

<a href="#" id="btnDone">CLOSE</a>

<script type="text/javascript">
$(document).ready(function () {
    $("#btnDone").click(function () {
 //I've replaced next string
 // $(this) here means another object $("#btnDone")
  $("#form-dialog").dialog('close');
    });
});
</script>

</form>
</div>

回答by kurdtpage

Using $(this).dialog('close');only works inside the click function of a button within the modal. If your button is not within the dialog box, as in this example, specify a selector:

Using$(this).dialog('close');仅适用于模态内按钮的点击功能。如果您的按钮不在对话框中,如本例所示,请指定一个选择器:

$('#form-dialog').dialog('close');

For more information, see the documentation

有关更多信息,请参阅文档

回答by Michel Ayres


        $(document).ready(function () {
            $("#form-dialog").dialog({
                autoOpen: true,
                modal: true,
                width: 200,
                draggable: true,
                resizable: true,
                buttons: {
                    "Close": function () {
                        $("#idDialog").dialog("close");
                    }
                }
            });
        });

This will make you a button to close. you can also call the function close

这将使您成为一个关闭按钮。你也可以调用函数close

$("#idDialog").dialog("close");

in some function to do this. or even in a button/a

在某些功能中执行此操作。甚至在一个按钮/一个

< a href="javascript:void(0);" id="btnDone"   
                              onClick="$("#idDialog").dialog("close");">CLOSE</a>

EDIT:you need this to include your dialog into form:

编辑:您需要将对话框包含在表单中:

open: function (type, data) {
            $(this).parent().appendTo($("form:first"));
        }

回答by Vladimir Miroshnichenko

better way is "destroy and remove" instead of "close" it will remove dialog's "html" from the DOM

更好的方法是“销毁并删除”而不是“关闭”,它将从 DOM 中删除对话框的“html”

$(this).closest('.ui-dialog-content').dialog('destroy').remove();