【问题标题】:trigger click on data target modal does not trigger show.bs.modal触发点击数据目标模态不会触发 show.bs.modal
【发布时间】:2023-03-08 08:05:01
【问题描述】:

我正在尝试根据 url 中是否存在特定的查询字符串来自动打开引导模式。我通过在单击它时通常打开模式的按钮上调用 trigger('click') 来做到这一点:

<a href="" id="openmodal" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Open Modal</a>
$("#openmodal").trigger('click');

模态打开正常,但问题是以下通常在打开模态时触发的事件在通过触发单击打开模态时不再触发:

$(document).on('show.bs.modal', '#myModal', function(e){
    // This does not get fired anymore
});

知道为什么在模态打开按钮上调用 trigger('click') 时不会触发 show.bs.modal 事件吗?

感谢您的帮助

【问题讨论】:

    标签: jquery twitter-bootstrap bootstrap-4 bootstrap-modal eventtrigger


    【解决方案1】:

    您的事件绑定需要在触发代码之前,如下所示:

    $(function() {
        // You can also do
        // $('#myModal').on('show.bs.modal', function(e) {
        $(document).on('show.bs.modal', '#myModal', function(e) {
            console.info(e);
        });
        
        $("#openmodal").trigger('click');
    });
    

    演示: https://jsfiddle.net/davidliang2008/g51aLcxr/7/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2020-12-27
      相关资源
      最近更新 更多