简体   繁体   中英

bootstrap 4 modal function not working on click

I have a modal that when the button for it is clicked an action occurs for this example write to the console. The modal is opening but event is not happning.

HTML File

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

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

JS File

jQuery.noConflict();
(function($){
  $(function() {
    $('#myModal').on('show.bs.modal', function(e) {
      console.log('new button clicked');
    });
  });
})(jQuery);
<button type='button' data-toggle="modal" data-target="#myModal">Open modal</button>
<!-- The Modal -->
<div class="modal" id="myModal">

try above this will open your modal ... you just had two closing tags of button ie </button>

Your code works perfectly by the way

 $('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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM