Rails 3:如何通过javascript触发表单提交? [英] Rails 3: How to trigger a form submission via javascript?

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

问题描述

我有一个表单,大多数情况下只是作为普通表单提交,所以我不想在form_tag中设置:remote => true选项.

I have a form that for the most part just submits as a normal form, so I don't want to set in the form_tag the :remote => true option.

但是,在某些情况下,我希望能够将一个javascript函数发布到表单上,就像它是由:remote => true发布的一样.我需要在javascript中做什么才能做到这一点?

However, under certain circumstances I'd like to be able have a javascript function post the form as if it had been posted by :remote => true. What would I need to do in javascript to accomplish this?

推荐答案

我刚刚尝试了此方法,该方法肯定有效,如果表单具有remote => true,则只需删除data-remote属性即可正常使用javascript即提交

I just tried this which definitely works, if the form has remote => true, just remove the data-remote attribute to submit normally w/ javascript ie

$('input').click(function(){ $('#form').removeAttr('data-remote') });

我猜测相反的方法可能会起作用,即,如果表单没有remote => true,那就做

I'm guessing the opposite would probably work ie if the form doesn't have remote => true just do

$('input').click(function(){ $('#form').attr('data-remote',true)});

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

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