2017-08-23 136 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腳本我試圖去運行

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

我使用的引導3.3和jQuery v1.11.2

讓我知道如果有更多的信息將有助於解決問題 在此先感謝

+1

你的模式是不可見的,因此'show.bs.modal'事件永遠不會觸發。如果你顯示模式,它工作正常: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

在這裏,你去了一個解決方案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>

我猜你缺少bootstrap.min.js文件。

1

您的代碼觸發模式和警報。你記得包含JQuery嗎?

試試這個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>