6

Ruby on Rails で、2 つのパーシャルを更新しようとしています。

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 ファイルの最後に?

誰でもこれに対する解決策を見ることができますか?

事前にご挨拶と感謝

4

1 に答える 1

2

次のように、 product.jsでjquery の live 関数を使用してみてください。

$(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 がバインドをやり直します。

それが役に立てば幸い!

于 2012-12-19T20:27:26.510 に答える