javascript 等待 jquery 对话框返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15484957/
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-10-27 00:56:57  来源:igfitidea点击:

wait till jquery dialog box returns

javascriptjquery

提问by vjk

I need to open a popup window on clicking a button and used jquery dialog for this.

我需要在单击按钮时打开一个弹出窗口,并为此使用 jquery 对话框。

    $(document).ready(function(){
    $("#dialog-form").dialog({
        autoOpen : false,
        height : 300,
        width : 350,
        modal : true,
        buttons : {
            "Add" : function() {
                $("#tag1").text($("#textArea").val());
                $(this).dialog("close");
            },
            Cancel : function() {
                $(this).dialog("close");
            }
        },
        close : function() {
            $("#textArea").val("");
        }
    });

});

    function openWindow(){
        $("#dialog-form").dialog("open");
        statement1;
        statement2;
        }


<button id="add" onclick="openWindow()">Add</button>

problem over here is when i click the button dialog box is opened, but before even i enter some text in the dialog box statement1 and statement2 are getting executed and then focus is coming to the dialog box.

这里的问题是当我单击按钮时对话框被打开,但在我在对话框中输入一些文本之前,语句 1 和语句 2 被执行,然后焦点来到对话框。

How can i make the statement1 and statement2 to execute only after dialog box returns?

如何让 statement1 和 statement2 只在对话框返回后才执行?

I don't want to add the statement1 and statement2 to the "Add" function. The reason for not adding the statements in the "Add" function is because i have multiple buttons and each of these should first open the dialog box and then will execute different set of statements.

我不想将 statement1 和 statement2 添加到“添加”函数中。没有在“添加”函数中添加语句的原因是因为我有多个按钮,每个按钮都应该先打开对话框,然后再执行不同的语句集。

回答by Aaron Blenkush

Easy fix would be to use the closecallback:

简单的解决方法是使用close回调:

$(document).ready(function () {
    $("#dialog-form").dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "Add": function () {
                $("#tag1").text($("#textArea").val());
                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            $("#textArea").val("");
            //statement1 -- won't fire until dialog is closed
            //statement2 -- won't fire until dialog is closed
        }
    });
});

function openWindow() {
    $("#dialog-form").dialog("open");
}

Another thing to consider would be $.Deferred

另一件要考虑的事情是 $.Deferred

回答by Nitin Vashisth

This can be achieved by the following way:-

这可以通过以下方式实现:-

$('#mydialog').dialog("open");
$('#mydialog').load('serverURL',server_data_variable, function() {
    myfunction();
});

This will execute the function once the dialog loading is done.It will register the callback to be executed post dialog done.The variable server_data_variableis optional and is supposed to be used only if user wants to send some data otherwise it can be skipped as.well.

这将在对话框加载完成后执行该函数。它将注册要在对话框完成后执行的回调。变量server_data_variable是可选的,应该仅在用户想要发送一些数据时使用,否则可以跳过它。好。

回答by TiagoBrenck

I have an example for you:

我给你举个例子:

$(".selector").click(function () {
        var dialog = $('<div title="Title"></div>').dialog({
            open: function (event, ui) {
                $.ajax({
                    url: 'www.google.com.br',

                    cache: false,
                    success: function (html) {
                        $(dialog).html(html);
                    },
                    error: function () {
                        $(dialog).remove();
                        alert("Some Error MSG");
                    }
                });
            },
            close: function () {
                $(dialog).remove();
            },
            resizable: false,
            width: 500,
            modal: true
        });
    });

In this case, the dialog is receiving the HTML result, only after it opens.

在这种情况下,对话框只有在打开后才会接收 HTML 结果。

回答by abc

Call callback function after "open" clause in the dialog setup.

在对话框设置中的“open”子句之后调用回调函数。

 modal: true,
 resizable: false,
 resize: 'auto',
 close: dialogCloseFunction,
 **open: function(){if(itemid) {showDailogSides(itemid);}** if(!siteParams[3]) {$(".detailSideClass").hide(); $(".addToChartinDialog").hide();}},
 //hide: {effect: "fadeOut", duration: 5000}
 show: { effect: "fade", duration: 1000 } //drop blind fade fold slide clip