javascript 提交表单,关闭弹窗并刷新父页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12593863/
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
Submit form, close popup window and refresh parent page
提问by Robert John Concepcion
popup1.php
popup1.php
<script>
function pepz(pageURL, title,w,h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}
</script>
<a href="" onclick="pepz('<?php echo"popup2.php"; ?>', 'myPop1',465,410)"><?php print"Click"; ?></a></td>
popup2.php
popup2.php
<form action='#' method='post'>
<input type='submit'>
<form>
After clicking the submit button in a form in a popup, I need the popup to close and the parent page which called the popup to refresh.
单击弹出窗口中表单中的提交按钮后,我需要关闭弹出窗口并刷新调用弹出窗口的父页面。
What am i doing wrong? thank you :D
我究竟做错了什么?谢谢:D
回答by Esben Tind
The easy way would be to add the following attribute to your submit button:
简单的方法是将以下属性添加到您的提交按钮:
onClick="window.parent.location.reload();window.close()"
回答by tony gil
Call your popup like this from your parent page:
从您的父页面像这样调用您的弹出窗口:
window.open("foo.html","windowName", "width=400,height=400,scrollbars=no");
Submit your form using a function. Control the window closing event and make it refresh parent page.
使用函数提交表单。控制窗口关闭事件并使其刷新父页面。
<form action='#' method='post' name='my_form'>
<input type="button" value="Upload" onclick="closeSelf();"/>
</form>
<script>
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
function closeSelf(){
document.forms['my_form'].submit();
window.close();
}
</script>