【问题标题】:Is there a way to submit a form, and then open a new window?有没有办法提交表单,然后打开一个新窗口?
【发布时间】:2015-04-22 01:23:55
【问题描述】:

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

【问题讨论】:

  • 添加您目前尝试过的代码
  • 现在我正在使用元刷新将它们从页面中删除
  • 但我更愿意打开一个新窗口?是否有将用户从页面中移除但在新窗口中的元刷新版本?
  • 你应该使用DOM或者更好fancybox
  • 我对此很陌生,这到底是什么意思?

标签: javascript form-submit new-window


【解决方案1】:

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

例子:

// 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'); 
});

所有主流浏览器都将允许上面打开一个新窗口/标签。

JSFiddle

【讨论】:

    【解决方案2】:

    您可以通过在表单上放置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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多