【问题标题】:How to close Bootstrap 5 Modal by using js code如何使用 js 代码关闭 Bootstrap 5 Modal
【发布时间】:2022-01-26 04:57:09
【问题描述】:

目标:
显示警报消息后,使用 javascript 代码关闭 bootstrap 5 模态。

问题:
在使用 javascript 代码显示警报消息后,我无法使函数 myFunction 正常工作并关闭模式。

信息:
*你不应该使用'data-bs-dismiss="modal"'

JSBIN:
https://jsbin.com/sileqajiho/edit?html,js,output

谢谢!


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="container mt-3">
  <h3>Modal Example</h3>
  <p>Click on the button to open the modal.</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    Open modal
  </button>
</div>

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

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Modal Heading</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        Modal body..
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" onclick="myFunction()" >Start</button>
      </div>

    </div>
  </div>
</div>

<script>

  function myFunction() {
    alert("test")
  }
  
<script>


</body>
</html>

var myModal = document.getElementById('staticBackdrop');
var modal = bootstrap.Modal.getOrCreateInstance(myModal)
modal.show()

【问题讨论】:

  • 脚本标签未关闭,这就是您的代码无法正常工作的原因。

标签: javascript bootstrap-5 bootstrap5-modal


【解决方案1】:

好吧,据我所知,您必须使用 new ,这是敬酒的示例

var myToast = new bootstrap.Toast(toastDiV, opts)
myToast.show()

因为它们是你必须引用一个新实例的类

【讨论】:

  • 不行!
【解决方案2】:

https://jsbin.com/ciqocavoge/edit?html,js,output


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container mt-3">
  <h3>Modal Example</h3>
  <p>Click on the button to open the modal.</p>
  
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    Open modal
  </button>

</div>

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

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Modal Heading</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
            <button type="button" onclick="test1()">Close me via script</button>

      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" onclick="myFunction22()" >Start</button>
      </div>

    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
  
  function test1() {
    alert("sdf");
    modalInstance.hide()
  }
  
</script>  
  
  
</body>
</html>


-------------

var modalInstance = null;
var modelElem = document.querySelector('#myModal');
modelElem.addEventListener('shown.bs.modal', function (){
  modalInstance = bootstrap.Modal.getInstance(modelElem);
});
猜你喜欢
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 2017-12-04
  • 2017-12-14
  • 1970-01-01
  • 2012-12-04
相关资源
最近更新 更多