6

在 ruby​​ on rails 中,我正在尝试更新 2 个部分。

显示.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

显示.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 元素。使用 live,即使替换元素,jQuery 也会为您重新进行绑定。

希望能帮助到你!

于 2012-12-19T20:27:26.510 回答