简体   繁体   English

Rails + jQuery-使用jQuery提交不带Submit按钮的表单

[英]Rails + jQuery - Using jQuery to submit the form w/o the Submit button

Given: 鉴于:

<%=form_for [:project, @note], :remote => true do |f| %>

I'd like to create a jquery bind that automatically saves every few seconds. 我想创建一个自动保存每隔几秒钟的jquery绑定。 I'm not worried about the timing part yet, just how to use jquery to submit the form automatically (ie, not submit button click by the user.) 我还不担心计时部分,只担心如何使用jquery自动提交表单(即,无需用户单击提交按钮)。

I tried this, but it isn't working. 我试过了,但是没有用。 Suggestions? 有什么建议吗?

$('form[data-remote]').live('submit', function (e) {
    alert(1);
    e.preventDefault();
});

Thanks! 谢谢!

So you are talking about something like Autosave, right? 所以您正在谈论类似自动保存的事情,对吗?

Maybe you want to specify a Javascript interval to this. 也许您想为此指定一个Javascript间隔。 An example could be: 例如:

var autosave = window.setInterval("autosaveForm()", 1000);

function autosaveForm() {
  $('form[data-remote]').submit();
}

What this does is to call autosaveForm() every second and submits the form. 这是每秒调用一次autosaveForm()并提交表单。 To stop the autosave you could just use clearInterval like this: 要停止自动保存,您可以像这样使用clearInterval

window.clearInterval(autosave);

I hope this helps. 我希望这有帮助。

$('form[data-remote]').submit()将提交表单。

Start with setInterval: 从setInterval开始:

  setInterval(transmit, 3000);

Which calls this function: 哪个调用此函数:

function transmit(){
 // get the form field data
 var params = {
   formField1:$("input#formField1").val(),
   formField2:$("input#formField2").val()
 }

 // make the ajax call
 var url = "/controller/action";
   $.post(url, params,
     function(response, status){
     //display results(response, status);
   });
}

What version of jQuery are you using? 您正在使用哪个版本的jQuery? Are you using IE? 您正在使用IE吗? The early versions of live() didn't work with IE for form submits. live()的早期版本不适用于IE进行表单提交。 (See http://github.com/rails/jquery-ujs/issues/issue/8 ) (请参阅http://github.com/rails/jquery-ujs/issues/issue/8

I think I ended up using this: 我想我最终使用了这个:

if (jQuery.browser.msie){
    $("form[data-remote]").delegate("input[type='submit'],button[type='submit'],input[name='commit']", 'click', function(e) {
        $(this).parents("form[data-remote]").submit();
        if(e.preventDefault) e.preventDefault();
    });
}

in a project. 在一个项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM