60

モーダル内に外部リンクがあり、ユーザーがリンクをクリックした後にモーダルを非表示にしたい。それ、どうやったら出来るの?

これが私のコードです:

<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 do a search on google.com 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>
4

5 に答える 5

121

onclickモーダル非表示呼び出しをイベントにバインドする必要があります。

jQuery を使用していると仮定すると、次の方法でそれを行うことができます。

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

また、ドキュメントの読み込みが完了した後にクリック イベントがバインドされていることを確認してください。

$(function() {
   // Place the above code inside this block
});
enter code here
于 2012-10-16T12:56:38.983 に答える
14

スクリプトを削除し、HTML を変更します。

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

編集: 現在、この機能はブートストラップにまだ存在しないため、これは機能しないことに注意してください。こちらの問題を参照してください

于 2012-10-16T12:57:41.737 に答える
6

を使用しdata-dismiss="modal"ます。私が v3.3.5 を使用している Bootstrap のバージョンでは、 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
于 2016-06-28T15:13:28.987 に答える
0

図のようにします。

  $(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>

于 2018-11-13T20:00:22.030 に答える