如何从textarea中提交带有ENTER键的AJAX表单?

时间:2022-11-24 10:23:59

I'm developing a Ruby On Rails 2.3.8 application and I would like to know how to submit a remote_form_for form when users press the ENTER key within the textarea.

我正在开发一个Ruby On Rails 2.3.8应用程序,当用户在textarea中按下ENTER键时,我想知道如何提交remote_form_for表单。

Here is the form code:

这是表单代码:

  <% remote_form_for :post, PostComment.new, :url => save_outside_comment_path(post_id), :html => { :method => :put} do |f| %>
     <%= f.text_area :comment, :rows => 1, :id => "txtOutsideComment_#{post_id}", :class => "new-reply-text" %>
  <% end %>

Here is the jQuery function that does a submit, but not AJAX:

下面是一个jQuery函数,它做了一个提交,但不是AJAX:

jQuery('.new-reply-text').keypress(function(e) {
    if (e.keyCode == 13 && !e.shiftKey) {
        e.preventDefault();
        this.form.submit();
    }
});

I already do the page update from my controller, do I don't want to specify any syntax like success: from this jQuery function. I just want it to submit the remote_form_for using AJAX.

我已经从我的控制器进行了页面更新,我不想指定任何语法,比如success:从这个jQuery函数。我只是想让它提交remote_form_for使用AJAX。

1 个解决方案

#1


6  

I think you're asking how to do an AJAX request instead of a full page submit. If so

我认为您正在询问如何执行AJAX请求,而不是提交完整的页面。如果是这样的话

jQuery('.new-reply-text').keypress(function(e) {
  if (e.keyCode == 13 && !e.shiftKey) {
    e.preventDefault();
    jQuery.ajax({
      url: "/my/URL",
      data: jQuery(this).form.serialize(),
      success: function(){},
      dataType: "json"
    });
  }
});

You can save a few parameters if you use .get http://api.jquery.com/jQuery.get/

如果使用,可以保存一些参数。

or .post http://api.jquery.com/jQuery.post/

或. post http://api.jquery.com/jQuery.post/

Which are wrappers for .ajax http://api.jquery.com/jQuery.ajax/

哪些是.ajax http://api.jquery.com/jQuery.ajax/的包装器

I'm not a rails guy so you'll need to update the class/id of the first jQuery object to find the remote_form_for

我不是rails人员,所以您需要更新第一个jQuery对象的类/id以找到remote_form_for

#1


6  

I think you're asking how to do an AJAX request instead of a full page submit. If so

我认为您正在询问如何执行AJAX请求,而不是提交完整的页面。如果是这样的话

jQuery('.new-reply-text').keypress(function(e) {
  if (e.keyCode == 13 && !e.shiftKey) {
    e.preventDefault();
    jQuery.ajax({
      url: "/my/URL",
      data: jQuery(this).form.serialize(),
      success: function(){},
      dataType: "json"
    });
  }
});

You can save a few parameters if you use .get http://api.jquery.com/jQuery.get/

如果使用,可以保存一些参数。

or .post http://api.jquery.com/jQuery.post/

或. post http://api.jquery.com/jQuery.post/

Which are wrappers for .ajax http://api.jquery.com/jQuery.ajax/

哪些是.ajax http://api.jquery.com/jQuery.ajax/的包装器

I'm not a rails guy so you'll need to update the class/id of the first jQuery object to find the remote_form_for

我不是rails人员,所以您需要更新第一个jQuery对象的类/id以找到remote_form_for