2013-02-09 81 views
0

我試圖從實際打開的模態視圖打開新的引導模態視圖。從模態打開bootstrap模態

我用下面的模板來打開我的所有不同模態:當我按下一個模式內的按鈕

<a href='<%= url_for :controller => :customers, :action => :new %>' data-target='#myModal' type='button' class='btn' role='button' data-toggle='modal'> 
    <i class="icon-plus"></i> new Customer</a> 

實際上,:

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-header"> 
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
    <h3 id="myModalLabel">&nbsp;</h3> 
    </div> 
    <div class="modal-body"> 
    <div class="alert alert-error" style="text-align: center;"> 
     <strong>ERROR!</strong><br />You should not be able to see this text!^ 
    </div> 
    </div> 
</div> 
<script> 
    $("a[data-target=#myModal]").click(function(ev) { 
     ev.preventDefault(); 
     var target = $(this).attr("href"); 

     // load the url and show modal on success 
     $("#myModal .modal-body").load(target, function() { 
      $("#myModal").modal("show"); 
     }); 
    }); 
</script> 

然後,我打電話給我的情態動詞與回報率像那樣,沒有任何反應。

回答

3

首先,當您使用自己的模態加載時,不應該使用data-toggle="modal"屬性,因爲遠程頁面將被加載兩次(通過您和插件)。

其次,當你綁定的事件,它僅使用的元素已經在頁面上,除非您使用delegated events,如:

$(document).on('click','a[data-target="#myModal"]', function(ev) { 

}); 

公告簡單的報價'周圍的選擇和雙引號"周圍的屬性值。

從那裏,它應該工作:Demo (jsfiddle)

+0

我想包括您的代碼示例。現在我遇到了一些問題:1.沒有任何東西在模態中打開。我認爲原因是,我使用RoR也許? 2.打開模式並再次關閉後,我無法在無需重新加載頁面的情況下打開它。 – Adrian 2013-02-11 12:15:52

+0

@Adrian嘗試將JS放入'$(function(){/ * code * /});'以便它等待['onDomReady'](http://api.jquery.com/jQuery/# jQuery3),就像[那個小提琴](http://jsfiddle.net/Sherbrow/FH7md/5/)。檢查JS錯誤。我猜你的綁定永遠不會發生,或'.load(target)'調用可能失敗。檢查瀏覽器的網絡檢查員。 – Sherbrow 2013-02-11 18:49:06

+0

感謝那個tipp!現在它工作了! – Adrian 2013-02-12 10:58:07