如何在按下取消按钮时关闭模态弹出窗口?

时间:2022-04-10 19:41:08

I have a simple modal and I was wondering how can I only allow the modal to be closed when clicking the "X" button or the cancel button. Now when the user clicks outside the modal area. Here's my code:

我有一个简单的模态,我想知道如何只在单击“X”按钮或取消按钮时才允许关闭模态。现在,当用户点击模态区域外。这是我的代码:

 <div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- 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>

    </div>
  </div>

</div>

2 个解决方案

#1


2  

use this..

用这个..

$('#myModal').modal({
    backdrop: 'static',
    keyboard: false
})

or directly put it in your div

或直接把它放在你的div中

<div id="myModal" class="modal hide" data-backdrop="static" data-keyboard="false">

#2


0  

Using data-backdrop="static" data-keyboard="false" on the opener button will prevent closing modal with ESC or clicking the background.

在开启器按钮上使用data-backdrop =“static”data-keyboard =“false”将阻止使用ESC关闭模式或单击背景。

#1


2  

use this..

用这个..

$('#myModal').modal({
    backdrop: 'static',
    keyboard: false
})

or directly put it in your div

或直接把它放在你的div中

<div id="myModal" class="modal hide" data-backdrop="static" data-keyboard="false">

#2


0  

Using data-backdrop="static" data-keyboard="false" on the opener button will prevent closing modal with ESC or clicking the background.

在开启器按钮上使用data-backdrop =“static”data-keyboard =“false”将阻止使用ESC关闭模式或单击背景。