如何使用ENTER键从文本区域提交AJAX表单



我正在开发Ruby On Rails 2.3.8应用程序,我想知道当用户在文本区域内按ENTER键时如何提交remote_form_for表单。

这是表单代码:

  <% 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 %>

下面是jQuery函数,它可以进行提交,但不能进行AJAX:

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

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

我想你在问如何进行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"
    });
  }
});

如果使用.get,可以保存一些参数http://api.jquery.com/jQuery.get/

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

它们是.ajax的包装器http://api.jquery.com/jQuery.ajax/

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

最新更新