【问题标题】:Bootstrap modal opens wrong oneBootstrap modal打开错误的一个
【发布时间】:2015-10-22 15:53:50
【问题描述】:

在我的页面中,我有两个模态:

<div id="modal1" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">My title 1</h4>
      </div>
      <div class="modal-body">
        My content 1
      </div>
      <div class="modal-footer">
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div id="modal2" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">My title 2</h4>
      </div>
      <div class="modal-body">
        My content 2
      </div>
      <div class="modal-footer">
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

我用

打开它们
$('#modal1').modal('show');
$('#modal2').modal('show');

只要我需要它们。

问题是不管modalid是什么,第一个modal总是打开的。 我试着把

$('.modal').on('hidden', function(){
    $(this).data('modal', null);
});

在我的代码中,但没有任何改变。

(我从来没有同时展示这两个模态。)

我正在使用 Backbonejs:当像这样单击按钮时,我会打开 modal2

module.exports = Backbone.View.extend({

events : {
    'click #my_button' : 'show_modal_2'
},

initialize: function(){
    this.render();
},

render: function(){
    var template = my_template; 
    this.$el.html( template );
    $('#modal1').modal('show');
},
show_modal_2 : function(){
    $('#modal2').modal('show');
}

});

当页面在render中加载时打开模态编号1。

modal1 完美打开,而modal2 打开时打开modal1

【问题讨论】:

  • 您需要显示更多调用$('#modalid').modal('show'); 的脚本。一个 jsfiddle 示例会非常有帮助。
  • 应该是 $('#' + modalid)
  • 您不想使用“$('modal1')”和“$('model2')”吗?
  • 向我们展示更多您的 HTML 和 JS,以及您在何处以及如何使用此模式。帮助你会更容易。
  • 您的代码就像您描述的那样工作。我看不出有什么问题。检查我的小提琴:http://jsfiddle.net/abL9qf39/

标签: jquery twitter-bootstrap backbone.js bootstrap-modal


【解决方案1】:

我发现我做错了什么。

在我的代码中,我像这样动态构建modal-body

$('.modal-body').html(my_html);

问题是我正在像这样更改两种模式,所以modal2 看起来就像modal1。 我通过这样做解决了:

$('#modal1 .modal-body').html(my_html);

【讨论】:

    猜你喜欢
    • 2013-10-31
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    相关资源
    最近更新 更多