javascript-引导模式-单击“号召性用语”按钮时关闭模式

我的模态中有一个外部链接,我希望在用户单击链接后隐藏该模态。 我怎么做?

这是我的代码:

<div class="modal hide fade" id="modalwindow">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Title</h3>
  </div>
  <div class="modal-body">
    <p>You need to search on google for that.</p>
  </div>
  <div class="modal-footer">
    <a id="closemodal" href="https://www.google.com" class="btn btn-primary" target="_blank">Launch google.com</a>
  </div>
</div>

<script type="text/javascript">
    $('#closemodal').modal('hide');
</script>
Tomas Jacobsen asked 2020-08-04T23:08:54Z
4个解决方案
91 votes

您需要将模式隐藏调用绑定到click事件:

$('#closemodal').click(function() {
    $('#modalwindow').modal('hide');
});

还要确保在完成文档加载后,单击事件已绑定:

$(function() {
   // Place the above code inside this block
});
jsalonen answered 2020-08-04T23:09:14Z
13 votes

删除脚本,然后更改HTML:

<a id="closemodal" href="https://www.google.com" class="btn btn-primary close" data-dismiss="modal" target="_blank">Launch google.com</a>

编辑:请注意,由于引导程序中尚不存在此功能,因此当前无法使用。 在这里查看问题。

Praveen Kumar Purushothaman answered 2020-08-04T23:09:38Z
4 votes

使用data-dismiss="modal"。在Bootstrap版本中,我使用的是v3.3.5,当将data-dismiss="modal"添加到所需的按钮(如下所示)时,它将精美地调用我的外部Javascript(JQuery)函数,并神奇地关闭了模式。 太甜了,我担心我不得不在另一个函数中调用一些模式隐藏并将其链接到实际的工作函数

 <a href="#" id="btnReleaseAll" class="btn btn-primary btn-default btn-small margin-right pull-right" data-dismiss="modal">Yes</a>

在某些外部脚本文件中,并且在我准备好的文档中,当然还有单击该标识符ID的功能

 $("#divExamListHeader").on('click', '#btnReleaseAll', function () {
               // Do DatabaseMagic Here for a call a MVC ActionResult
ChristianProgrammer answered 2020-08-04T23:10:03Z
0 votes

如图所示。

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

    $('#myBtn').on('click', function(){
      $('#myModal').modal('show');
    });
    
  });
<br/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!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 rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Activate Modal with JavaScript</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" id="myBtn">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>
      
    </div>
  </div>
  
</div>

Guilherme Rigotti answered 2020-08-04T23:10:22Z
translate from https://stackoverflow.com:/questions/12915222/bootstrap-modal-close-modal-when-call-to-action-button-is-clicked