bootstrap 4模态函数在单击时不起作用

时间:2019-07-19 15:15:39

标签: javascript jquery bootstrap-4

我有一个模态,当单击该按钮的按钮时,此示例将执行一个操作,将其写入控制台。模态是开放的,但事件没有发生。

HTML文件

<button type='button' data-toggle="modal" data-target="#myModal">Launch modal</button>
Open modal
</button>

<!-- The Modal -->
<div class="modal" id="myModal">

JS文件

jQuery.noConflict();
(function($){
  $(function() {
    $('#myModal').on('show.bs.modal', function(e) {
      console.log('new button clicked');
    });
  });
})(jQuery);

2 个答案:

答案 0 :(得分:-1)

<button type='button' data-toggle="modal" data-target="#myModal">Open modal</button>
<!-- The Modal -->
<div class="modal" id="myModal">

尝试上面的操作将打开您的模式...您只有两个关闭按钮的标签,即</button>

答案 1 :(得分:-2)

您的代码可以很好地工作

$('button[data-target="#myModal"]').on( "click", function() {
  console.log('clicked')
});

$('#myModal').on('shown.bs.modal', function () {
  console.log('modal opened')
})
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<button type='button' data-toggle="modal" data-target="#myModal">Open modal</button>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>