【问题标题】:Bootstrap 3.3.2 Modal events fire multiple timesBootstrap 3.3.2 模态事件多次触发
【发布时间】:2015-05-17 05:06:05
【问题描述】:

Bootstrap Modal Events 触发多次,或者更确切地说总是比之前多一次。我将模态事件包装在一个 click 函数中,该函数现在返回模态 ID。

如何确保事件始终只触发一次,并且仅在点击函数调用它时触发?

jQuery

$('.img-modal').on('click', function () {

// returns #modal-Id
var modalIdClicked = $(this).attr('data-target')
console.log ('modal id clicked from .img-modal = '+ modalIdClicked);

console.log ('.img-modal clicked');

    $(modalIdClicked).on('show.bs.modal', function(event) {

        console.log ( 'show.bs.modal');

    });    

});

默认 Bootstrap 3.3.2 模态 HTML

<div class="col-md-7">
    <img class="img-modal img-responsive cursor-pointer" data-toggle="modal" data-target="#modal-1" src="www" alt="image">
</div>

<!--  Modal -->
<div class="modal fade top-space-0" id="modal-1" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content cursor-pointer" data-toggle="modal" data-target="#modal-1">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="#modal-1">CLOSE &times;</button>
                <h1 class="modal-title">Modal Title</h1>
            </div>

            <div class="modal-body">
                <div class="img-center">
                    <img class="img-modal img-responsive" src="wwww" alt="image">
                </div>
            </div>

            <div class="modal-footer">
                <a href="#" class="btn btn-default" data-dismiss="modal">Close</a>
            </div>
        </div>
    </div>
</div>
<!--  Modal end -->

我确实看过jQuery click events firing multiple times 和 Bootstrap 3 - Remotely loaded modal creates duplicate javascript events 似乎可以通过 .off 方法实现。

所以在这种情况下使用 $(modalIdClicked).off().on('show.bs.modal', function(event) { 就可以了。

有人可以向我解释一下为什么在这种情况下究竟需要这种.off 方法吗?我无法理解点击是如何传递的。

每次点击是否会再次到达 Bootstrap JS?为什么在没有.off 方法的情况下多次触发事件?

谢谢。

(我正在尝试从一本书中学习 jQuery,如果有人真的有一本好的书,甚至是唯一一本的书来阅读这方面的内容,那么请给我留个便条,那里有这么多,而且都声称自然是最好的..谢谢。)

【问题讨论】:

  • 在我的例子中,不得不放“.off()”,因为相同的模态是从不同的地方加载的。感谢您的回答!

标签: jquery twitter-bootstrap-3


【解决方案1】:
modal.off().on('show.bs.modal', function(){
    modalBody.load(remote_content);
});

就我而言,我面临着多个 remote_loads。 '.off()' 帮助我将之前绑定的事件解除绑定到目标模式。

【讨论】:

  • $('.detail_status').on('change', function () { 'content here' });运行了几次。 .off 成功了。谢谢。
【解决方案2】:

为什么在没有.off 方法的情况下多次触发事件?

因为每次单击元素 .img-modal 时,您都会为 show.bs.modal 事件附加 一个新的 事件侦听器。这就是为什么每次点击都会触发一次事件show.bs.modal。

有人可以向我解释一下为什么在这种情况下需要这种.off 方法吗?

.off() 方法只是删除之前附加的事件。


您不需要使用.off() 方法。

不要在每个点击事件上为事件show.bs.modal附加一个事件侦听器,您应该为所有类.modal的元素添加一个事件侦听器:

Example Here

$('.modal').on('show.bs.modal', function (event) {
    console.log(this.id);
});

在show.bs.modal 事件监听器中,this 绑定到显示的.modal 元素;因此您可以确定显示的是哪个模态。在上面的示例中,this.id 将是 modal-1。

【讨论】:

  • 很好的解释,特别感谢您回答的第二部分!我将不得不围绕那部分的一些事情来思考。让.img-modal上的点击监听器找到被点击的modal,还包括遍历dom和children等等。如果不是我最初计划对模态事件做的所有事情,这就会发生很大变化。谢谢你,谢谢你,谢谢你!自学还是通过书本/学校?如果通过书,请哪一个?最后一次,感谢您的中肯解释!
  • @lowtechsun 不客气——很高兴它有帮助。为了回答你的问题,我是自学的。 Stack Overflow 几乎教会了我所知道的一切。我开始在没有太多知识/经验的情况下回答问题;但是在 1338 回答之后,我学到了一些东西;)对于核心 JavaScript,我强烈推荐 "JavaScript the Good Parts"(这是一个沉重/深入的阅读)。 MDN这个网站也不错。。没看过jQuery方面的书,主要参考jQuery API documentation。
  • 你解释得很好,它帮助我解决了我的问题。每天从堆栈溢出中学习新东西。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
  • 1970-01-01
  • 2016-03-14
相关资源
最近更新 更多