Ajax请求的表单未提交

时间:2011-12-02 08:55:56

标签: jquery ruby-on-rails ajax forms

我用Ajax请求加载图片和单选按钮。之后我想提交此单选按钮,如果我点击<%= sg.submit'确认步骤1'%>。提交适用于所有形式,但不适用于Ajax。有什么问题?

信息:Ajax内容被加载到div。

查看 - new.html.erb

<%= form_for :site, :html => { :id => "form_all" }  do |sg| %>
  ...
  <%= sg.submit 'Confirm Step 1' %>
  <%= sg.text_field :subject %>
  ...

  <div id="pics_<%= deal.DealID %>">
  </div

  <%= link_to "Get pictures", {:action => "new", :format => :js }, :remote => true %> 
<% end %>

Controller - site_controller.rb

def new
  respond_to do |format|
    format.html { }
    format.js {}
  end
end

JS - new.js.erb

$("#hidden_div").html('<%= escape_javascript( render :partial => "hidden" ) %>');
var div_name = $("#hidden_field").val();
var substr = div_name.split('"');
$(substr[1]).html('<%= escape_javascript( render :partial => "pic" ) %>');

JS - application.hs

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

$(document).ready(function() {
  $("#form_all").submitWithAjax();
})

部分 - _pic.html.erb

<% @Pics.each do |pic| %>
  <a href="<%= pic.PublicServerURI %>" target="_blank"><%= image_tag(("#{pic.PublicServerURI}"), :size => "100x80") %></a>                                       
  <input value="<%= pic.PublicServerURI %>" id="site_pics" name="pics[DealID[<%= @DealID %>]]" type="radio">Use Deal with that Picture<br> 
<% end %> 

1 个答案:

答案 0 :(得分:0)

由于您无法嵌套表单标记,因此无法将form_for嵌套到我的知识中。您也不应该为每个表单使用多个提交按钮。

我可能会尝试像

这样的东西
link_to "Get pictures", new_foo_path, :remote => true

通过回复替换内容。

相关问题