Javascript表单提交打开新窗口选项卡,然后重定向父页面

Javascript form submit open new window tab, and then redirect parent page

本文关键字:然后 重定向 选项 表单提交 新窗口 窗口 Javascript      更新时间:2023-09-26

当我提交表单时,我试图打开一个新的窗口选项卡,并将值传递到新的窗口标签中。然后我需要父页面重定向到下一个页面。

问题是表单值没有传递到新窗口选项卡。我怎么能用我现在拥有的以下代码做到这一点?

<form class="form" id="promo_form" method="post">
<input type="text" name="first_name" id="first_name">
<input type="text" name="last_name" id="last_name">
<input type="text" name="zipcode" id="zipcode" maxlength="5">
<button type="submit" id="promo_form_button" class="submit">Continue</button>
</form>

$(document).ready(function($){
var form = document.getElementById("promo_form");
    function goToUrl(){
        window.open("https://site.com/new-tab", "_blank");
        window.location = "https://site.com/parent-next-page"
    }
    document.getElementById("promo_form_button").onclick = goToUrl;
});

当表单可以针对新选项卡/窗口本身时,为什么要使用window.open。

HTML:

<form target="_blank" method="get" action="http://google.com" id="myForm">
    <label for="q">Search: </label><input type="text" id="q" name="q" />
    <input type="submit" value="search" />
</form>

JavaScript:

document.getElementById("myForm").onsubmit = function() {
    window.location.href = "http://www.jsfiddle.net";
    return false;
};

示例:

JSFiddle