有没有办法提交一个表单,然后打开一个新的window?

Is there a way to submit a form, and then open a new window?

我希望能够在不使用 window.open 的情况下执行此操作,因为有人告诉我这会被弹出窗口拦截器拦截。是否可以使用 javascript 以某种方式更改提交按钮(例如添加 target="_blank")或其他方式来实现此目的?

您可以通过在表单上放置 onsubmit 来控制提交表单时发生的情况,或者使用 <a target="_blank"> 作为表单提交按钮。

如果您选择第一个选项:

<form onsubmit="formSubmit()"> </form>
<script>function formSubmit(){code here}</script>

或第二个选项:

<form id="form">
<!--place your inputs here -->
<a href="google.com" target="_blank" onclick="document.getElementById('form').submit()"></a>
</form>

如果 window.open 由于用户操作而被调用,您的新 window 将不会被浏览器阻止。

示例:

// Find the button you want to bind to 
var button = document.getElementById('test');
// Add a click event listener
button.addEventListener('click', function(){
    window.open('http://www.google.com'); 
});

所有主流浏览器都将允许以上内容打开一个新的 window / 标签页。

JSFiddle