javascript 如何将表单提交到新窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30835990/
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
How to submit form to new window?
提问by user4951834
I want to insert a form dynamically using JavaScript and then submit it and open target in new window.
我想使用 JavaScript 动态插入一个表单,然后提交它并在新窗口中打开目标。
Here is my code:
这是我的代码:
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", xxx);
form.setAttribute("onsubmit", "window.open('about:blank','Popup_Window','scrollbars=yes,width=550,height=520');");
form.setAttribute("target", "Popup_Window");
document.body.appendChild(form);
form.submit();
This code works -- it inserts the form dynamically and submits the form successfully. However, the form is opened in a new tab whereas I want it to open in a new window. Any idea what is up? Any suggestions on how to fix this? I'm open to jQuery as well.
这段代码有效——它动态插入表单并成功提交表单。但是,表单是在新选项卡中打开的,而我希望它在新窗口中打开。知道发生了什么吗?对于如何解决这个问题,有任何的建议吗?我也对 jQuery 持开放态度。
Thanks!
谢谢!
EDIT: I'm not sure why people are marking this as duplicate. Yes, it is on the same topic as the other question but the answers are not related -- the claimed duplicate issue had a new line that was messing up the code, I don't but my code still won't work...
编辑:我不确定为什么人们将其标记为重复。是的,它与另一个问题属于同一主题,但答案不相关——声称的重复问题有一个新行,它弄乱了代码,我没有,但我的代码仍然无法工作......
采纳答案by Danziger
I've marked this as duplicate because I thought your problem was about how to send a form to a popup window. Here is how to do it without using the onsubmitevent:
我已将此标记为重复,因为我认为您的问题是关于如何将表单发送到弹出窗口。以下是不使用onsubmit事件的方法:
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", xxx);
function submitToPopup(f) {
var w = window.open('', 'form-target', 'width=600, height=400, any-other-option, ...');
f.target = 'form-target';
f.submit();
};
document.body.appendChild(form);
submitToPopup(form);
So instead of using the onsubmitevent to create the popup from there, you create it first, just before sending the form, and then send the form to it.
因此,不是使用onsubmit事件从那里创建弹出窗口,而是在发送表单之前先创建它,然后将表单发送给它。
回答by Muhammad Umer
You could do this instead [link],
- open a new window
- insert form in it
- make it submit
which will navigate to url
var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", "http://www.yahoo.com"); function popitup() { newwindow = window.open('', 'name', 'width=800,height=600'); if (window.focus) { newwindow.focus() } newwindow.document.body.appendChild(form); newwindow.document.forms[0].submit(); return false; } popitup();
- 打开一个新窗口
- 在其中插入表格
- 让它提交
这将导航到 url
var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", "http://www.yahoo.com"); function popitup() { newwindow = window.open('', 'name', 'width=800,height=600'); if (window.focus) { newwindow.focus() } newwindow.document.body.appendChild(form); newwindow.document.forms[0].submit(); return false; } popitup();
回答by Harsha Kuchampudi
To my knowledge, most browsers and ad blockers will block this kind of script from opening a new window because it is autorun. However, if you implement it like this where the user must click a link: JSFiddleit is more likely to open in a new window.
据我所知,大多数浏览器和广告拦截器会阻止这种脚本打开一个新窗口,因为它是自动运行的。但是,如果您像这样实现它,用户必须单击链接:JSFiddle,它更有可能在新窗口中打开。
Using jQuery:
使用jQuery:
$('.open-popup').click(function(e) {
e.preventDefault();
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "");
document.body.appendChild(form);
form.submit();
window.open(this.href, '_blank', 'width=300,height=200');
});
HTML:
HTML:
<a href="about:blank" class="open-popup">Click Me</a>
Also, some browsers have preferences that disable opening in a new window automatically. So you may want to explore alternatives or simply ask users to open the link in a new tab.
此外,某些浏览器具有禁用自动在新窗口中打开的首选项。因此,您可能想要探索替代方案或简单地要求用户在新选项卡中打开链接。
回答by boateng
If you want to always keep a popup "focused" you can use a trick originally found here. By rapidly open/close/open it we ensure it's always behaves like new window and grabs focus.
如果您想始终保持弹出窗口“集中”,您可以使用最初在这里找到的技巧。通过快速打开/关闭/打开它,我们确保它始终表现得像新窗口并抓住焦点。
<form name=f1 method=post action=test.html>
<input type=text name=name value='123'>
<input type='submit' value='Submit'
onclick="if(window.open('','reuse_win') != null) {
window.open('','reuse_win').close();
}; this.form.target='reuse_win'; return true;">
</form>
Also works with a link.
也适用于链接。
<a href="http://stackoverflow.com" onclick="
if(window.open('','reuse_win') != null) {
window.open('','reuse_win').close();
}" target="reuse_win">Always opens in the same window and FOCUS</a>

