使用渲染部分发布具有事件的响应不起作用

ReB*_*eBa 6 javascript jquery ruby-on-rails ruby-on-rails-3.2

在ruby on rails上,我试图更新2个部分.

show.html.erb:

<div id="filters"><%= render :partial => "pricelistfilters" %></div>
Run Code Online (Sandbox Code Playgroud)

pricelistfilters.html.erb:

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

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
    });
  });
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

show.js.erb

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

我渲染的项目非常好.但是,当我对渲染的项目得到正确的响应时,它就不再发送ajax了.因此,我选择的项目上的事件已经消失,而我确信我已经使用"selectionchanges();"重置了它们.在我的show.js.erb文件的末尾?

任何人都可以看到这个解决方案吗?

提前问候和感谢

小智 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
    });
  });
}
Run Code Online (Sandbox Code Playgroud)

基本上,您将替换首先将“change”事件绑定到的 HTML Dom 元素。在 live 中,即使替换元素,jQuery 也会为您重新进行绑定。

希望能帮助到你!