2012-04-07 46 views
2

我有3個文件:阿賈克斯beforeSend只能一次on Rails的

index.html.erb是我的主頁

我呈現的部分,_relationship.html.erb指標裏面,有一個表單使用以下行:

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

我也有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'); 
    }) 

表單每次發送一次,值改變。

我想使用beforeSend選項顯示加載gif。

我想(在index.html.erb如果我把它放在_relationships部分,它不會在所有的工作):

jQuery("#relationships_form_1").bing("ajax:beforeSend", function() { 
$("#relationship_save").show(); 
}); 

但它只能一次。

然後,我添加了軌道選項

:before => '$("#relationship_save").show();' 

之前它的工作原理,再一次,只有一次。

我甚至嘗試jQuery的

jQuery("#relationships_form_1").on("ajax:beforeSend", function() { 
$("#relationship_save").show(); 
}); 

的。對,它仍然有效只有一次。

形式發送每一次,值存儲每一次,beforesend只能一次

有什麼想法?

回答

7
jQuery(document).on("ajax:beforeSend", "#relationships_form_1", function() { 
    $("#relationship_save").show(); 
}); 

因爲你的ajax加載的表單與初始表單不一樣。

+0

太棒了。非常感謝 – wachichornia 2012-04-07 15:12:44