需要允许表单重新提交Rails AJAX [英] Need to allow forms to be resubmitted Rails AJAX
问题描述
我正在编写一个应用程序,希望能够通过AJAX将人员添加到部门中,但是尽管我可以删除很多次,但是我不能添加多次.该表格似乎无法多次提交. 我正在使用部分(包含两种形式的表格).唯一的区别是只有一个添加表单"和许多删除表单"(因为您可以同时在多个部门工作).
I'm writing an app where I want to be able to add people to a department via AJAX, but though I can remove many times, I can't add more than once. It seems that the form can't be submitted multiple times. I'm using a partial (that contains the two types of forms). The only difference is that there is only one "add-form" and many "remove-forms" (since you can be in many departments at once).
<td><%= form_tag remove_department_person_path(:id => @person.id, :department => department), :method => :post, :class => "remove_form", :remote => true do %>
<% submit_tag "Remove", :class => "btn"%><% end %></td>
已生成多次
<%= form_tag add_department_person_path(:id => @person.id), :method => :post, :remote => true, :class => "add_form" do %>
<td>
<%= select_tag 'department', options_for_select(@unassigned)%>
</td>
<td>
<%= text_field_tag :title %>
</td>
<td>
<%= submit_tag "Add", :class => "btn add-btn"%>
</td>
<% end %>
生成一次.我的JavaScript是
Is generated once. My javascript is
$('.remove_form').on('ajax:success', function(event,data) {
d = jQuery.parseJSON(data)
$('#departments').html(d.html)
return false
})
$('.add_form').live('ajax:success', function(event,data) {
d = jQuery.parseJSON(data)
$('#departments').html(d.html)
return false
})
每次重新加载部分时都会重新运行,所以这不应该成为问题.有什么建议吗?
This is re-run every time the partial is reloaded so that shouldn't be the problem. Any suggestions?
推荐答案
发现问题(至少部分地)是由于rails处理AJAX的方式引起的-事件处理程序仅关闭了一次.
Discovered that the problem was (at least partially) due to the way that rails handles AJAX - the event handlers only go off once.
function updateForms(data) {
$('#departments').html($("<div>"+data+"</div>").find("#departments").html())
setAjax()
return false
}
function setAjax() {
$('#deplist').on('submit', '.remove_form', function(e) {
$.rails.handleRemote( $(this) );
e.preventDefault();
});
$('.add_form').on('submit', function(e) {
$.rails.handleRemote( $(this) );
e.preventDefault();
});
$('.remove_form').on('ajax:success', function(event,data) {
updateForms(data)
})
$('.add_form').on('ajax:success', function(event,data) {
updateForms(data)
})
}
另一个堆栈溢出问题为我提供了此代码的基础,如果可以再次找到它,我将链接至该代码.
I was given the basis for this code from another stack overflow question and I'll link to that if I can find it again
这篇关于需要允许表单重新提交Rails AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!