Ajax beforeSend只在Rails上运行一次

wac*_*nia 2 ajax jquery ruby-on-rails

我有3个文件:

index.html.erb是我的主页

在索引中我渲染了一个部分的_relationship.html.erb,并且有一个包含以下行的表单:

<%= form_tag('/relationships/update/', :remote => true, :id => "relationships_form_#{@count}") do %>
Run Code Online (Sandbox Code Playgroud)

我也有index.js.erb(relationship.html在index.html.erb中)

jQuery("#relationships_form_1").bind("ajax:success", function() {
  $("#box_relationships").hide().html("<%=escape_javascript(render('relationships/relationships'))%>").fadeIn(2000, 'swing');
  })
Run Code Online (Sandbox Code Playgroud)

每次发送表单,并更改值.

我想使用beforeSend选项显示加载gif.

我试过(在index.html.erb上.如果我把它放在_relationships部分,它根本不起作用):

jQuery("#relationships_form_1").bing("ajax:beforeSend", function() {
$("#relationship_save").show();
});  
Run Code Online (Sandbox Code Playgroud)

但它只工作一次.

然后,我添加了之前的rails选项

:before => '$("#relationship_save").show();'
Run Code Online (Sandbox Code Playgroud)

它再次只能工作一次.

我甚至在jquery上尝试了.on

    jQuery("#relationships_form_1").on("ajax:beforeSend", function() {
$("#relationship_save").show();
});
Run Code Online (Sandbox Code Playgroud)

它仍然只能工作一次.

每次发送表单,每次存储值,之前只发送一次

有什么想法吗?

Mik*_*iet 8

jQuery(document).on("ajax:beforeSend", "#relationships_form_1", function() {
  $("#relationship_save").show();
});
Run Code Online (Sandbox Code Playgroud)

因为你的ajax加载形式与初始形式不同.