2014-02-18 24 views
0

我嘗試ajax裏面的產量,所有工作正常,但是當我實現ajax替換html在application.html.erb(佈局)ajax不工作,並且不能替換html。如何用rails中的ajax替換application.html.erb中的html?

我application.html.erb:

<li id="header_notification_bar" class="dropdown"> 
    <a id="click_notif" data-toggle="dropdown" class="dropdown-toggle" data-remote="true" href="#"> 
    <i class="fa fa-bell-o"></i> 
    <span id="notif_count" class="badge bg-warning"></span> 
    </a> 
    <ul id="notif_content" class="dropdown-menu extended notification"> 

    </ul> 
</li> 

<script> 
    $(document).ready(function() { 
    $('#notif_content').html("<%=j render 'layouts/load_notif' %>"); 
    $("#click_notif").click(function(){ 
     $('#notif_count').html("<%=j render 'layouts/notif_count' %>"); 
    }); 
    });   
</script> 

如果內部收益率的AJAX工具,在控制器的動作應該添加format.js和我創建action_name.js.erb文件,但如果在application.html.erb是我應該做的???

回答

0

請幫幫我,謝謝你試過這樣:

$(document).ready(function() { 

    $('#notif_content').html("<%=j render 'layouts/load_notif' %>"); 

    $(document).on("click", "#click_notif", function(){ 
     $('#notif_count').html("<%=j render 'layouts/notif_count' %>"); 
    }); 

    }); 

代表

雖然你的問題是相當模糊的,典型的問題,您有阿賈克斯是因爲你正在將新元素加載到DOM中,所以JS事件綁定器無法附加相應的事件

我不想像我已經寫會開箱的代碼,但它基本上依賴於.delegate原則 - 選擇「容器」元素,並委託從

由於事件容器在DOM加載時存在,這意味着您可以使用它來處理新的元素。您確實需要提供您的AJAX代碼來幫助我們創建更好的答案