Ruby rails – jquery ajax提交表单

发送ajax请求是否正确? 此代码无效,我需要在此处更改哪些内容? 有没有更好的方式发送ajax表单?

 true, :id => 'create_item' do %> 

40, :size => 70 %> 10, :size => 10, :value => Time.now.utc.strftime("%Y-%m-%d") %> 10, :size => 10, :value => Time.now.localtime.strftime("%H:%M:%S") %>

50, :size => 50 %>

"new" %>

"javascript:submitForm()" %>

function submitForm() { $.ajax({type:'POST', url: '/item/create', data:$('#create_item').serialize(), success: function(response) { $('#create_item').find('#item').html(response); }}); return false; }

使用它肯定会起作用

  <%= form_for(:customer, :url => {:controller => "subscribers", :action => "change_password"}, :html => {:id => 'edit_password_form', :method => :get, :onsubmit => "return false;"}) do |f| %> <%= hidden_field_tag "ids", :id => "ids"%> 
<%= f.password_field :password, :class=> 'fields', :placeholder => 'Password' %>
<%= f.password_field :password_confirmation, :class=> 'fields', :placeholder => 'Password Confirmation' %>
<%= submit_tag "CHANGE PASSWORD", :id => "edit_password_btn" %>
<% end %>

和jquery代码

   

并且享受….

我能够解决它。 我的表单被隐藏在该视图上的几个div标签下。 我把它拿出来,把它保存在一个单独的div中。 然后修改了jQuery。 然后它奏效了。

查看 –

 <%= form_tag item_create_path, :remote => true, :id => 'create_item' do %> 

<%= label_tag :"Name" %> <%= text_field_tag :name, nil, :maxlength => 40, :size => 70 %> <%= label_tag :"Date" %> <%= text_field_tag :date, nil, :maxlength => 10, :size => 10, :value => Time.now.utc.strftime("%Y-%m-%d") %> <%= label_tag :"Time" %> <%= text_field_tag :time, nil, :maxlength => 10, :size => 10, :value => Time.now.localtime.strftime("%H:%M:%S") %>

<%= label_tag :Description %> <%= text_field_tag :description, nil, :maxlength => 50, :size => 50 %>

<%= hidden_field_tag :type, nil, :value => "new" %>

<%= submit_tag " Create ", :id => "create_item_button", :onclick=>"javascript:submitForm()" %>

<% end %> In assets/item.js - function submitForm(){ $('#create_item_button').click(function() { $.ajax({ type: 'POST', url: "/item/create", data:$('#create_item').serialize(), error: function(){ }, success: function(data){ $("#item_form").hide(); $('#view_item').html(data); }, complete: function (){ } }); return false; }); }