简体   繁体   English

以编程方式显示 Bootstrap 模态窗口

[英]Programmatically show Bootstrap modal window

I am using JavaScript.我正在使用 JavaScript。 I don't know how to open bootstrap modal.我不知道如何打开引导模式。 It works fine when I click on below button:当我点击下面的按钮时它工作正常:

 <button type="button" class="btn btn-primary" data-toggle="modal" id="btnOpenPopup" data-target="#myModal">
     Open modal
 </button>

But I want to open it without user event, it should be opened programmatically.但是我想在没有用户事件的情况下打开它,应该以编程方式打开它。

I tried with $("#btnOpenPopup").click();我试过$("#btnOpenPopup").click(); and (document.getElementById('btnOpenPopup')).click();(document.getElementById('btnOpenPopup')).click(); but it's not working.但它不起作用。

Below is my modal.下面是我的模态。

<div class="modal fade" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
          Modal body..
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Try this.试试这个。 Its working for me.它为我工作。

If You have a login button and If login details is correct then You want to show this popup.如果您有登录按钮并且登录详细信息正确,那么您想显示此弹出窗口。 Then do something like this.然后做这样的事情。

<button type="button" class="btn btn-primary" onclick="checkUserDetails()"> Login</button>

Then In your script tag or your js file然后在您的脚本标签或您的 js 文件中

function checkUserDetails() {
  // Check user details here 
  if (loginDetail) {  
    $("#YourModelName").show();   // show modal/popup
  } else {
    // do somethimg if logindetails is not valid
  }
}

Hope it help you.希望对你有帮助。

You can toggle a modal by using the modal() function.您可以使用modal()函数切换模modal()

$("#myModal").modal(show | hide)

This is the same for Bootstrap 3 and 4.这与 Bootstrap 3 和 4 相同。

See source here .请参阅此处的来源。

For Bootstrap 4.x it look like the good way is对于 Bootstrap 4.x,看起来好方法是

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

Source: http://getbootstrap.com/docs/4.1/getting-started/javascript/来源: http : //getbootstrap.com/docs/4.1/getting-started/javascript/

Try putting the modal() function inside document.ready尝试将 modal() 函数放在 document.ready 中

$(document).ready(function() {
    $('#myModal').modal('show');
});

Heres the working Fiddle这是工作中的小提琴

You can do it manually by .hide() and .show()你可以通过做手工.hide().show()

$(function(){
   $("#myModal").hide();
   $("#btnOpenPopup").click(function(){
         $("#myModal").show();
    });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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