javascript 打开新的弹出窗口并返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13301908/
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
open new popup window and return value
提问by beshoy Mounir
I am trying to open a new popup window, insert values into database, after that return one value to current window. After I open a new popup window and click return, it returns the value but when I click on submit and return it after that, the value doesn't get returned. I think that's because the new window gets refreshed by the submit button. That's why it doesn't return the value.
我正在尝试打开一个新的弹出窗口,将值插入数据库,然后将一个值返回到当前窗口。在我打开一个新的弹出窗口并单击返回后,它会返回该值,但是当我单击提交并在此之后返回时,该值不会被返回。我认为这是因为提交按钮刷新了新窗口。这就是它不返回值的原因。
Main Page
主页
<form></form>
<form>
<input name="maparea" size="2" type="TEXT">
<input onclick='targetitem = document.forms[1].maparea; dataitem = window.open("popup.php", "dataitem", "toolbar=no,menubar=no,scrollbars=yes"); dataitem.targetitem = targetitem' value="Get Value" type="button">
</form>
Popoup window
弹出窗口
<script>
function select_item(item){
targetitem.value = item;
top.close();
return false;
}
</script>
<form action="" method="post">
<input type="submit" name="sub" value="Submit" />
<input type="button" name="re" value="Return" onclick='return select_item("3")' />
</form>
Any solution for that? I want to submit what I want first after that return the value
有什么解决办法吗?我想先提交我想要的,然后返回值
回答by albattran
In the popup, hook an onclick event on your submit button so it executes before the submit. Then in the onclick handler do:
在弹出窗口中,在提交按钮上挂钩 onclick 事件,以便在提交之前执行。然后在 onclick 处理程序中执行以下操作:
window.opener['dataitem'] = <your return value>;
Then after the submit, your parent window will have that value, and you can access it like this:
然后在提交后,您的父窗口将具有该值,您可以像这样访问它:
var somevariable = window['dataitem'];
回答by David Weinraub
How about this?
这个怎么样?
- Open your popup.
- In the popup: submit the form via AJAX(to avoid a page refresh).
- In the popup: In the success handler for your AJAX call, grab the desired value, pass it back using
window.opener
, then close the popup.
- 打开您的弹出窗口。
- 在弹出窗口中:通过 AJAX提交表单(以避免页面刷新)。
- 在弹出窗口中:在 AJAX 调用的成功处理程序中,获取所需的值,使用 将其传回
window.opener
,然后关闭弹出窗口。
回答by Raja O
function setColor(color){ if (opener && !opener.closed){ opener.document.theForm.theField.value = color; opener.focus(); } window.close(); } ... <td width="30" bgcolor="#FFFF00" onclick="setColor(this.bgColor)"> </td>
Read more at http://www.codingforums.com/showthread.php?t=61319#gkH9pd6gdgvxYqQZ.99