Javascript jQuery 弹出窗口返回值给父级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2353011/
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
jQuery Popup Window Return Value to Parent
提问by Mark Richman
How can I accomplish the following using jQuery: Open a popup window that returns a value to the parent window when a link in the child window is clicked, close the child window, and then have the parent automatically submit a form based on the value returned?
如何使用 jQuery 完成以下操作:打开一个弹出窗口,当单击子窗口中的链接时向父窗口返回一个值,关闭子窗口,然后让父窗口根据返回的值自动提交表单?
I realize that the jQuery Dialog is a popular solution, but I require a popup window because the window's contents need to be navigable, and I want to avoid using an iframe in the jQuery Dialog.
我意识到 jQuery Dialog 是一种流行的解决方案,但我需要一个弹出窗口,因为窗口的内容需要可导航,并且我想避免在 jQuery Dialog 中使用 iframe。
The popup window is going to be used to collect more than one value, ultimately to be returned as a delimited string to the parent, but this data collection needs to occur prior to the submission of the parent window's form. If there were a standard design pattern for an "Entity Picker", this would be it.
弹出窗口将用于收集多个值,最终作为分隔字符串返回给父窗口,但此数据收集需要在提交父窗口表单之前发生。如果有一个“实体选择器”的标准设计模式,就是这样。
This needs to work in IE8, FF3.6, Safari 4, and Chrome 5.
这需要在 IE8、FF3.6、Safari 4 和 Chrome 5 中工作。
Thanks, Mark
谢谢,马克
回答by iman1003
Here is my solution:
这是我的解决方案:
var parent = $(parent.document.body);
$(parent).find('input#valStore').val(theVal);
$(parent).find('form#myForm').submit();
window.close();
回答by Rune
In your newly opened browser window you could try something like
在您新打开的浏览器窗口中,您可以尝试类似的操作
$("#mylink").click(function(){
value = /* get some value */
window.opener.$("#myform .somehiddenfield").val(value);
window.opener.$("#myform").submit();
window.close();
});
DISCLAIMER: I haven't tested this in any browser.
免责声明:我没有在任何浏览器中测试过这个。

