【问题标题】:Bootstrap show.bs.modal event not working on FirefoxBootstrap show.bs.modal 事件在 Firefox 上不起作用
【发布时间】:2015-08-12 08:08:22
【问题描述】:

我正在使用引导事件在显示模式对话框后触发。在 Chrome 上,一切都像一个魅力,但在 Firefox 中,该事件永远不会触发。我的 Firefox 版本是 38.05。这是我的代码:

    $(document).on('show.bs.modal', function () {
        $(elementEvents).each(function( i, event ) {
            console.log("binding event index " + i +" for  type " + event.type);
            currentElement.bind(event.type,event);
        });
    });

如果我改变这一行

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

通过这个作品

    $("*").on('show.bs.modal', function () {

知道为什么 Firefox 不解释文档吗?

【问题讨论】:

标签: jquery twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

您必须将event 绑定到实际的模态。

$('#exampleModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var recipient = button.data('whatever') // Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this)
  modal.find('.modal-title').text('New message to ' + recipient)
  modal.find('.modal-body input').val(recipient)
});

这是一个有效的example

或者你可以将它绑定到类:

$('.modal').on('show.bs.modal', function (event) {
    alert('here I am!');
    console.log(event);
});

$('.modal').on('hidden.bs.modal', function (event) {
    alert('bye bye!');
    console.log(event);
});

如图所示jsFiddle

【讨论】:

  • 它是所有模态对话框的通用 js,所以不可能。当然,除非我使用模态类,你给了我一个想法,谢谢!
  • 不,使用模态它也不起作用 $(".modal").on('hidden.bs.modal', function ()
  • 您的代码中一定有其他内容,因为它确实如此work
  • 是的,问题是一些对话框是在触发打开时从服务器加载的,但是我的通用 js 在页面渲染时运行,所以此时模式不在DOM 已经。到目前为止,我最好的方法是使用委托 $("*").delegate('.modal','hidden.bs.modal', function () {
  • 对我有帮助。+1
【解决方案2】:

我刚刚遇到了这个问题...我将代码移到了 jQuery(document).ready() 中,现在它可以为我工作了。

jQuery(document).ready(function () {
    jQuery('.modal').on('hidden.bs.modal', function (e) {
        // whatever you want to do
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多