提交表单时,我试图打开一个新的窗口选项卡并将值传递到新的窗口选项卡中.然后,我需要父页面重定向到下一页.
问题在于表单值未传递到新的窗口选项卡.
我现在该如何使用下面的代码来做到这一点?
<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;
};
例: