Javascript 弹出窗口返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4867289/
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
Popup window return value
提问by yesiamconfused
I hope someone may be kind enough to help me with this issue.
我希望有人可以帮助我解决这个问题。
What I am trying to do I return a value from a popup window into the parent window that launched it using javascript.
我想要做的是将一个值从弹出窗口返回到使用 javascript 启动它的父窗口中。
What I have tried is calling (as read on various websites)
我试过的是打电话(在各种网站上读到)
window.opener.document.forms[0].textField.value = 'value'
but while this does not produce any errors it does not change the field value.
但是虽然这不会产生任何错误,但它不会改变字段值。
I have tried searching on the net for a solution to this but there as so many sites relating to 'pop up return value' on google, results dating back to 2000, many seem to conflict each other so I am somewhat confused.
我曾尝试在网上搜索此问题的解决方案,但谷歌上有很多与“弹出返回值”相关的网站,结果可追溯到 2000 年,许多似乎相互冲突,所以我有些困惑。
Ideally what I would prefer to do is have the pop up window wait for a decision to be made (either yes or no), and have a true or false value return to the calling function from the parent window. As the reason for this, is that I have a form the uses onsubmit to call a javascript function to confirm weather to continue on not. I was using a dialog to return a true false value, but now trying to do it with a pop up so I can include images and various other information.
理想情况下,我更喜欢让弹出窗口等待做出决定(是或否),并从父窗口向调用函数返回真或假值。这样做的原因是,我有一个表单,使用 onsubmit 调用 javascript 函数来确认天气是否继续。我正在使用一个对话框来返回一个真假值,但现在尝试通过一个弹出窗口来完成它,以便我可以包含图像和各种其他信息。
Hopefully I have explained this well enough and someone can help me with a solution.
希望我已经很好地解释了这一点,有人可以帮助我解决问题。
Thanks for your time.
谢谢你的时间。
回答by John K.
You can reference the window that opened the popup from the popup... In this example, something on the popup window calls this function (which exists on the popup window) passing in the "val"...
您可以从弹出窗口中引用打开弹出窗口的窗口...在此示例中,弹出窗口上的某些内容调用此函数(存在于弹出窗口中)传入“val”...
<script language="javascript">
function GetRowValue(val)
{
// hardcoded value used to minimize the code.
// ControlID can instead be passed as query string to the popup window
window.opener.document.getElementById("ctl00_ContentPlaceHolder1_TextBox2").value = val;
window.close();
}
</script>
回答by jpic
Here is the dissection of Django admin's approach: Howto: javascript popup form returning value for select like Django admin for foreign keys. It is undoubtedly more complicated, but eventually this technique will get you there.
以下是对 Django 管理员方法的剖析:Howto: javascript popup form returns value for select like Django admin for Foreign keys。毫无疑问,它更复杂,但最终这种技术会让你达到目标。
回答by jpic
use this in parent page
在父页面中使用它
var answer = window.open(<your page and other arguments>)
if (answer == 1)
do some thing
in child page
在子页面
window.returnValue = 1;
回答by nitin kumar
Try the following:
请尝试以下操作:
window.opener.document.getElementById("sizeId" + rn).value
Above size id will match the parent window id of input type where you want to return the values.
以上大小 id 将匹配您要返回值的输入类型的父窗口 id。