2017-08-23 22 views
0

モーダルが開かれたときにトリガーするコードをバインドしようとしています。私はモーダルを開くのに問題はありませんが、何らかの理由で私の出来事が誘発されていないと判断できません。ここでブートストラップモーダルイベントがトリガーされない

は私のモーダル

<div id="details" class="modal fade" style="display: none;" aria-hidden="true"> 
    <div class="modal-dialog modal-"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button class="close" type="button" data-dismiss="modal">×</button> 
      <h4 class="modal-title"></h4> 
      </div> 
       <div class="modal-body"> 
      <p id="modal-body" class="text-defualt text- text-normal text-left"></p> 
     </div> 
      <div class="modal-footer"></div> 
     </div> 
    </div> 
</div> 

そして、ここでは私のJSスクリプトである私は、ブートストラップ3.3とjQueryのv1.11.2

を使用してい

<script> 
    $(function(){ // let all dom elements are loaded 
     $("#details").on("show.bs.modal", function (e) { 
      alert("event fired"); 
     }); 
    }) 
</script>' 

を実行するために取得しようとしています

これ以上の情報が問題を解決するのに役立つかどうか教えてください ありがとうございます。

+1

あなたのモーダルが表示されていない試してみてください。モーダルを表示すると、うまく動作します:https://jsfiddle.net/u1pdunkf/ –

答えて

1

以下のコードを試してください: ボタンをクリックしてモーダルポップアップを開くと仮定します。それが動作するはずです、コードの下に追加します。 ここでは、このfiddle

<script> 
    $(function(){ // let all dom elements are loaded 
    $(document).on('show.bs.modal','#details', function() { 
     alert('hi'); 
     }); 
    }); 
</script> 
1

を参照してください、あなたは私はあなたがbootstrap.min.jsファイルが欠落していると思いソリューションhttps://jsfiddle.net/kxr2subr/

$(function(){ // let all dom elements are loaded 
 
    $("#details").on("show.bs.modal", function (e) { 
 
     alert("event fired"); 
 
    }); 
 
    
 
    $('button').click(function(){ 
 
    \t $('#details').modal({show: true}); 
 
    }); 
 
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<button> 
 
Open Modal 
 
</button> 
 

 
<div id="details" class="modal fade" style="display: none;" aria-hidden="true"> 
 
    <div class="modal-dialog modal-"> 
 
     <div class="modal-content"> 
 
      <div class="modal-header"> 
 
       <button class="close" type="button" data-dismiss="modal">×</button> 
 
      <h4 class="modal-title"></h4> 
 
      </div> 
 
       <div class="modal-body"> 
 
      <p id="modal-body" class="text-defualt text- text-normal text-left"></p> 
 
     </div> 
 
      <div class="modal-footer"></div> 
 
     </div> 
 
    </div> 
 
</div>

で行きます。

1

あなたのコードは、モーダルとアラートを起動します。 JQueryを組み込むことを覚えましたか?

は、したがって、 `show.bs.modal`イベントが発火することはありません、この CodePen Demo

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 

 
<!-- Button trigger modal --> 
 
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#details"> 
 
    Launch demo modal 
 
</button> 
 

 
<div id="details" class="modal fade" style="display: none;" aria-hidden="true"> 
 
    <div class="modal-dialog modal-"> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <button class="close" type="button" data-dismiss="modal">×</button> 
 
     <h4 class="modal-title"></h4> 
 
     </div> 
 
     <div class="modal-body"> 
 
     <p id="modal-body" class="text-defualt text- text-normal text-left">I work</p> 
 
     </div> 
 
     <div class="modal-footer"></div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 

 
<script> 
 
    $(function() { 
 
    $("#details").on("show.bs.modal", function(e) { 
 
     alert("event fired"); 
 
    }); 
 
    }); 
 
</script>

関連する問題