在文本框中按 Enter 后,重定向到 newtab url - 绕过 Firefox 弹出窗口阻止

After hit Enter in a text box, redirect to newtab url - bypass Firefox pop-up block

这是我的脚本:

jQuery('#textfield').keydown(function (e){
    var text = document.getElementById('textfield').value;
        if(e.keyCode == 13){

            //document.getElementById('btntextfield').click();
            //window.location.href = "http://www.example.com";
            //window.open("https://www.w3schools.com");
            //setTimeout(function(){document.getElementById('btntextfield').click();}, 3 * 1000);
            alert("tsdks");
        }
});

html:

<input id="textfield" class="form-control" placeholder="search term..." type="text">
<a id="btntextfield" class="btn btn-carousel btn-lg" href=#" target="_blank" rel="noopener noreferrer">Search</a>

所以我正在尝试 - 在文本框中键入并点击 Enter 以重定向用户的新页面。我尝试了两种方法:

  1. 在文本框中输入并按回车键直接重定向到新页面

  2. 在文本框中键入以触发“btntextfield”按钮后

但每次都被 Firefox 拦截...

请帮忙

您可以使用 keypress 而不是 keydown 来绕过弹出块。您可以查看此笔以供参考:

https://codepen.io/ropilz/pen/qjoXNM

jQuery('#textfield')
  .keypress(function (e){
    if(e.keyCode == 13){
      document.getElementById('btntextfield').click();
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="textfield" class="form-control" placeholder="search term..." type="text">
<a id="btntextfield" class="btn btn-carousel btn-lg" href=#" target="_blank" rel="noopener noreferrer">Search</a>