如何通过AJAX提交我的远程更新表单? [英] How to submit my remote update form via AJAX?

查看:92
本文介绍了如何通过AJAX提交我的远程更新表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以编辑记录的表单,我希望它在提交时仍使用ajax,

I have a form that will edit the record, I want it to use ajax though when submitting,

= simple_form_for(@lesson, :html => {"data-type" => :html, :id=>'lesson_form'}, :remote=>true) do |lesson_form|

= lesson_form.button :submit, 'Update'

但是,在教训控制器中,在 update 动作中, request.xhr?的值为0,尽管我在以下位置将remote设置为true上面的表格选项; request.xhr?的计算结果不正确.

but, in the lessons controller at update action, the request.xhr? evaluates 0, although I am setting remote to true at the form options above; request.xhr? does not evaluate to true.

那么,我在这里想念什么?更新记录时如何使用ajax?将remote设置为true还不够吗?有什么想法吗?

So, what am I missing here ? How to use ajax when update my record ? Isn't setting remote to true enough ? Any ideas please ?

谢谢

推荐答案

您可以通过多种方式进行操作.其中一些是:

You can do it many ways. Some of them are:

如果使用GET传递表单数据,则可以使用jquery的ajax()并发送请求.那样您可以使用显式的ajax技巧,即以帧内作为通讯黑白格式并提交.您也可以使用它来验证表单,例如:检查用户名是否存在.在该场景中,您将使用JavaScript与iframe进行交互,并且该iframe将具有针对现有用户名的某些规则或标记,反之亦然.

If passing form data using GET, you can use jquery's ajax() and send the request. That way you can use an explicit ajax trick i.e, have in-frame as medium of communication b/w form and submit. You can use this to validate the form as well, for example: checking to see if the username exists or not. In that scenerio you will have javascript interacting with your iframe and that iframe will have certain rules or markups for username that exists and vice versa.

希望这会有所帮助.如果您愿意,我很高兴为您提供一些我前段时间写的示例代码.

Hope this helps. I am happy to provide you with some sample code i wrote a looooong time ago if you want.

这篇关于如何通过AJAX提交我的远程更新表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆