bootstrap模态展示前的绑定事件

Binding event before bootstrap modal show

我有一个简单的bootstrap模态

<div id="mymodal" class="modal fade">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Modal header</h3>
    </div>
    <div class="modal-body">
    ...
    <div>
</div>

我已经使用数据切换将它附加到一个按钮

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#mymodal"> Toggle</button>

现在,当单击按钮时,模式将显示。模态主体包含需要预填充的 div。我使用下面的代码来做同样的事情

$('#mymodal').on('shown.bs.modal', function(){
    generate_modal_body();    
})

但是上面的代码不起作用。令人惊讶的是,如果我在模态按钮上添加点击事件,它可以触发 generate_modal_body()。 我在这里检查过类似的问题,但找不到满意的答案。 如果我遗漏了一些明显的东西,请告诉我。

请试试这个

function openModal() {
      $('#mymodal').modal('show');
    }

请在文档就绪函数中附上 bootstrap 模态显示的事件。

<script type="text/javascript">
 $(document).ready(function () {
    $('#mymodal').on('shown.bs.modal', function() { 
       generate_modal_body();
    }) ;
 });
</script>

您必须使用正确的modal event