Rails 3:如何通过 javascript 触发表单提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4904101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 3: How to trigger a form submission via javascript?
提问by William Jones
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.
我有一个表单,在大多数情况下只是作为普通表单提交,所以我不想在 form_tag 中设置 :remote => true 选项。
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?
但是,在某些情况下,我希望能够有一个 javascript 函数发布表单,就好像它是由 :remote => true 发布的一样。我需要在 javascript 中做什么来完成这个?
采纳答案by sshillo
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
我刚试过这个肯定有效,如果表单有远程 => true,只需删除 data-remote 属性以使用 javascript 正常提交即
$('input').click(function(){ $('#form').removeAttr('data-remote') });
I'm guessing the opposite would probably work ie if the form doesn't have remote => true just do
我猜相反的可能会起作用,即如果表单没有远程 => true 就做
$('input').click(function(){ $('#form').attr('data-remote',true)});
回答by brachyceros
I'm sorta new to this but here goes...
我对这个有点陌生,但这里是......
rails.js (the jquery one at least) defines the following function to catch and submit forms:
rails.js(至少是 jquery 之一)定义了以下函数来捕获和提交表单:
$('form').live('submit.rails', function(e) { ... });
If you use the following it should trigger the same function (and if :remote => true, then it wont cause a page reload):
如果你使用以下它应该触发相同的功能(如果:remote => true,那么它不会导致页面重新加载):
$("#<yourformid>").trigger("submit.rails");
So, if you wanted to submit your form on a select change for example, you could set the above trigger call to the select's :onchange I would imagine.
因此,例如,如果您想在选择更改时提交表单,您可以将上面的触发器调用设置为选择的 :onchange 我可以想象。
回答by Shlomi Komemi
maybe u could try this:
也许你可以试试这个:
$('#form').submit();
回答by Preston Lee
If you're using jQuery you could do something like this to have the form auto-post on keyup events, or simplify it to trigger manually:
如果你使用 jQuery,你可以做这样的事情来让表单在 keyup 事件上自动发布,或者简化它以手动触发:
$(function() {
$("#live_search input").keyup(function() {
q = $('#search_text').val();
$.ajax({
beforeSend : function(request) { request.setRequestHeader("Accept", "text/javascript"); },
// Included so Rails responds via "format.js"
data : 'query=' + q,
success : function(data, textStatus, XMLHttpRequest) {
// alert(textStatus);
$("#live_search #results").html(data);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
// alert(errorThrown);
$('#annotation_dialog').html(XMLHttpRequest.responseText);
},
type : 'POST',
url : '/search/live'
});
return false;
});
});
回答by mltsy
In Rails 5 (which replaces jquery-ujs with rails-ujs), this is how you trigger the handler rails attaches to a form's submitevent:
在 Rails 5(用 rails-ujs 替换 jquery-ujs)中,这是触发处理程序 rails 附加到表单submit事件的方式:
var elem = document.getElementById('myform') // or $('#myform')[0] with jQuery
Rails.fire(elem, 'submit');
(Holy cow did it take me forever to figure that out!)
(天哪,我花了很长时间才弄明白这一点!)

