简体   繁体   中英

display bootstrap modal popup from javascript not working

I am trying to open a modal popup from a javascript block. This is a basic modal that I found in w3schools: http://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_modal&stacked=h

Here is my code :

           <script>
               function showModal(){         
                window.location.href = '#myModal';       
               }    
           </script>

      <!-- Modal -->
         <div class="modal fade" id="myModal" role="dialog">
         <div class="modal-dialog">

      <!-- Modal content-->
      <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>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>      

Instead of JavaScript you an use default bootstrap function

function showModal(){
    $('#myModal').modal('show')
}

window.location.href does not have what is known as a setter, which means it cannot be assigned a new value. It does have a getter as standard (a requirement, really), so you can get it.

If you want to open a link, try window.open("#myModal"); , or use jQuery with $('#myModal').modal('show');

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