Bootstrap $('#myModal').modal('show') is not working

后端 未结 15 2139
深忆病人
深忆病人 2020-12-03 13:50

I\'m not sure why but all the modal functions are not working with me. I checked the version and the load they are fine.

I keep getting this error message:

相关标签:
15条回答
  • 2020-12-03 14:24

    jQuery lib has to be loaded first. In my case, i was loading bootstrap lib first, also tried tag with target and firing a click trigger. But the main issue was - jquery has to be loaded first.

    0 讨论(0)
  • 2020-12-03 14:24

    Please also make sure that the modal div is nested inside your <body> element.

    0 讨论(0)
  • 2020-12-03 14:25

    Please,

    try and check if the triggering button has attribute type="button". Working example:

      <button type="button" id="trigger" style="visibility:visible;" onclick="modal_btn_click()">Trigger Modal Click</button>
    
      <!-- Modal -->
      <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog modal-sm">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
              <p>This is a small modal.</p>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>
        </div>
      </div>
    
       <script >
               function modal_btn_click() {
    
                   $('#myModal').modal({ show: true });
               }
       </script>
    
    0 讨论(0)
  • 2020-12-03 14:27

    use the object to call...

    <a href="#" onclick='$("#myModal").modal("show");'>Try This</a>
    

    or if you using ajax to show that modal after get result, this is work for me...

    $.ajax({ url: "YourUrl",
    type: "POST", data: "x=1&y=2&z=3",
    cache: false, success: function(result){
            // Your Function here
            $("#myModal").modal("show");
        }
    });
    
    0 讨论(0)
  • 2020-12-03 14:28

    After trying everything on the web for my issue, I needed to add in a small delay to the script before trying to load the box.

    Even though I had put the line to load the box on the last possible line in the entire script. I just put a 500ms delay on it using

    setTimeout(() => {  $('#modalID').modal('show'); }, 500);
    

    Hope that helps someone in the future. 100% agree it's prob because I don't understand the flow of my scripts and load order. But this is the way I got around it

    0 讨论(0)
  • 2020-12-03 14:29

    Try This without paramater

    $('#myModal').modal();
    

    it should be worked

    0 讨论(0)
提交回复
热议问题