如何将表格提交到新窗口? [英] How to submit form to new window?

查看:81
本文介绍了如何将表格提交到新窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript动态插入表单,然后提交它并在新窗口中打开目标。

I want to insert a form dynamically using JavaScript and then submit it and open target in new window.

这是我的代码:

            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();

此代码有效 - 它动态插入表单并成功提交表单。但是,表单在新选项卡中打开,而我希望它在新窗口中打开。有什么想法吗?对于如何解决这个问题,有任何的建议吗?我也对jQuery持开放态度。

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.

谢谢!

编辑:我不确定为什么人们将此标记为重复。是的,它与其他问题在同一主题上,但答案没有关系 - 声称的重复问题有一个新的行,搞乱了代码,我没有,但我的代码仍然无法正常工作......

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...

推荐答案

我将此标记为重复,因为我认为您的问题是关于如何将表单发送到弹出窗口。以下是不使用 onsubmit 事件的方法:

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 onsubmit event:

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);

所以不要使用 onsubmit 事件来从那里创建弹出窗口,首先在发送表单之前创建弹出窗口,然后将表单发送给它。

So instead of using the onsubmit event to create the popup from there, you create it first, just before sending the form, and then send the form to it.

这篇关于如何将表格提交到新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆