关闭模式对话框并在 javascript 中返回一个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14380985/
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
Close modal dialog with returning a value in javascript?
提问by Madura Harshana
I need to close the modal dialog with return a value in a same function
我需要关闭模态对话框并在同一函数中返回一个值
I cannot write code as follow,because when a returning a value, next line never be excuted,
我不能编写如下代码,因为当返回一个值时,下一行永远不会被执行,
function butOK_OnClick() {
return "OK";
window.close();
}
so is this the right way ?
那么这是正确的方法吗?
function butOK_OnClick() {
window.returnvalue = "OK";
window.close();
}
or what is the best way to do this ?
或者最好的方法是什么?
回答by jbabey
Assuming you're using window.showModalDialogto open the window (since window.opendoes not allow for return values), you'd just set the returnValueproperty of the modal and then set it to a variable in the opener.
假设您正在使用window.showModalDialog打开窗口(因为window.open不允许返回值),您只需设置returnValue模态的属性,然后将其设置为开启器中的变量。
Opener window:
开启窗口:
var returnedValue = window.showModalDialog(url);
Modal window:
模态窗口:
window.returnValue = 'foo';
window.close();
回答by Madura Harshana
you can use,
您可以使用,
$("#modalId", window.top.document).data("cancelled", true);
$("#modalId", window.top.document).data("returnValue", returnVal);

