【问题标题】:window.open opens multiple tabs instead of one in google chromewindow.open 在谷歌浏览器中打开多个标签而不是一个
【发布时间】:2021-07-02 22:06:46
【问题描述】:

我创建了一个带有搜索按钮的动态输入字段。起初,当我单击按钮时,仅打开选项卡。但是在保存后,如果我再次单击它,它会为同一个 url 打开多个选项卡。 选项卡的数量等于创建的动态输入字段的数量,这仅在 google chorme 中发生。

<button type="button" id='btn[]' name="btn" class='btn btn-default' ><i class="fa fa-search"></i></button>

<script>

      $(document).on('click', '[id^=btn]', function(f){
      f.preventDefault()
      console.log('vendor side');
      console.log(this.id);
      window.inputid = $(this).closest("tr").find("td input").attr("id");
      console.log(window.inputid);
      var myWin = window.open("/vendorlist/", "", "width=500,height=500");
  });

  </script>

创建动态字段后如何停止 window.open 以打开多个选项卡。请帮助。

【问题讨论】:

    标签: javascript jquery django dynamic window


    【解决方案1】:

    如果您再次单击,它将打开一个新选项卡。 如果您不想再收听 clicks 事件,只需使用 $(document).off 删除侦听器即可。

    <script>
        let listener = (f) => {
            f.preventDefault()
            console.log('vendor side');
            console.log(this.id);
            window.inputid = $(this).closest("tr").find("td input").attr("id");
            console.log(window.inputid);
            $(document).off('click', '[id^=btn]', listener);
            var myWin = window.open("/vendorlist/", "", "width=500,height=500");
        }
        $(document).on('click', '[id^=btn]', listener);
    </script>
    

    主要问题是用户无法理解按钮为什么不起作用,因此您可能需要将按钮设为灰色并添加一个信息标题。

    【讨论】:

    • 我希望单击按钮时只打开一个标签。我在 Internet Explorer 中检查了相同的代码,它工作正常,但在 google chorme 中它打开了多个标签而不是一个。
    • 好吧,我检查了您的代码,如果我只单击一次,则只会打开一个选项卡。也许在您的代码中的其他地方还有另一个 window.open。
    • 其实这是一个动态字段。第一次点击它时它工作正常。但是当我尝试修改它或添加新的动态字段时保存后,它会打开多次。
    • 也许尝试在你的选择器中更具体,也许多个监听器在同一个按钮上
    猜你喜欢
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 2020-06-18
    • 2016-08-15
    • 2015-09-16
    • 2023-03-05
    相关资源
    最近更新 更多