2012-12-19 45 views
6

在rails上的ruby中,我試圖更新2個partials。渲染部分有事件的帖子響應不起作用

show.html.erb:

<div id="filters"><%= render :partial => "pricelistfilters" %></div> 

pricelistfilters.html.erb:

<% @properties.each do |prop| %> 
    #Render the page on properties (and the newproperties) 
    #<select ...><option>...</option><option>...</option> 
    #</select> 
<% end %> 

products.js - >爲渲染部分

事件
$(window).ready(function(){ 
    selectionchanges(); 
}); 
function selectionchanges(){ 
    $('#filters select').change(function(){ 
    //Doing stuff to send with ajax 

    //Ajax: 
    $.ajax({ 
     url: document.URL, 
     type: 'POST', 
     data: params 
    }); 
    }); 
} 

products_controller.rb - >代碼來處理的變化作出

def show 
    #Changing the properties (and the pricelist properties) 
    @properties #is filled 
    @newproperties #is filled 
    respond_to do |format| 
    format.html 
    format.js 
    end 
end 

show.js.erb

$('#filters').html('<%= escape_javascript(render :partial => "pricelistfilters") %>'); 
selectionChanges(); 

我的渲​​染產品非常好的。但是,當我對渲染的項目有適當的響應時,它只是不再發送ajax。所以我選擇的項目上的事件已經消失了,而我確定我已經用「selectionchanges();」重置了它們。在我的show.js.erb文件的末尾?

任何人都可以看到這個解決方案嗎?

問候和感謝提前

+0

代碼塊表現得很奇怪:感謝@ jdl幫助我! – ReBa

+0

不客氣。我希望你能爲你的問題找到一個好的答案。 – jdl

+3

你有'selectionchanges'和'selectionChanges',是一個錯字? – RadBrad

回答

2

嘗試使用上product.js jQuery的直播功能,波紋管:

$(window).ready(function(){ 
    selectionchanges(); 
}); 
function selectionchanges(){ 
    $('#filters select').live('change', function(){ 
    //Doing stuff to send with ajax 

    //Ajax: 
    $.ajax({ 
     url: document.URL, 
     type: 'POST', 
     data: params 
    }); 
    }); 
} 

基本上,你要更換你先約束的HTML DOM的元素事件'改變'爲。通過直播,即使通過替換元素,jQuery也會爲您重新進行綁定。

希望它有幫助!

+0

應該看看這個。完美地工作!謝了哥們。 – ReBa