javascript 在提交时关闭弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14617199/
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
javascript close a popup on submit
提问by baash05
Ok. I've seen this question as been unanswered for over 10 years..
行。我已经看到这个问题 10 多年来一直没有答案。
How do you submit and close a popup in one button call.
您如何通过一键调用提交和关闭弹出窗口。
suggestions run thus.
建议就这样运行。
function saveNClose()
{
document.form.submit();
self.close();
}
this doesn't work because the submit has a redirect, and the self.close is never reached.
function closeLaterNSaveNow();
{
setTimeout("window.close()",5000);
document.form.submit();
}
same problem.. the close is never called because the script for the page is gone.
function closeNowNSubmit()
{
self.close();
document.form.submit();
}
the window closes, but nothing gets saved. server doesn't even get notified of anything..
<input class='btn btn-success' disabled='disabled' id='SubmitFinal' onclick='closeWindow()' type='submit' value='Finish'>
Well then there's no validation of the data. it submits before the function is called.
Anyone solve this? I found the same solutions dating back to 2002, and some as recent as this month. I shall continue to look, but I'm hoping someone has nailed it and that that someone answers here
有人解决这个问题吗?我发现了可以追溯到 2002 年的相同解决方案,有些甚至是本月。我会继续看,但我希望有人已经解决了它并且有人在这里回答
回答by esycat
A typical solution is to output a JS snippet that will close the window as a resulting page after submit.
一个典型的解决方案是输出一个 JS 片段,该片段将在提交后作为结果页面关闭窗口。
<script type="text/javascript">
window.close();
</script>
Another solution would be to submit form's data via AJAX, and close the window when request has finished. You can use jQuery Form pluginfor that:
另一种解决方案是通过 AJAX 提交表单数据,并在请求完成后关闭窗口。您可以为此使用jQuery Form 插件:
$("#myForm").ajaxForm(function() {
window.close();
})