如何使用ENTER键,从文本区域提交一个AJAX的形式?如何使用、文本、形式、区域

2023-09-11 00:51:15 作者:罂粟狠美ゝ也狠毒

我正在开发一个Ruby on Rails的2.3.8应用程序,我想知道如何提交 remote_form_for 的形式,当用户preSS的内ENTER键textarea的。

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.

下面的格式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 %>

下面是jQuery的功能,它一提交,但不AJAX:

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

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

我已经做了页面更新,从我的控制器,我不希望指定一个像成功的任何语法:从这个jQuery函数。我只是希望它提交 remote_form_for 使用AJAX。

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.

推荐答案

我觉得你问如何做一个AJAX请求,而不是一个完整的页面提交。如果是这样

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

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"
    });
  }
});

如果您使用可以保存几个参数,不用彷徨 http://api.jquery.com/jQuery.get/

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

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

其中包装了阿贾克斯 http://api.jquery.com/jQuery.ajax/

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

我不是一个轨道的人,所以你需要更新的第一个jQuery对象的类/ id来找到 remote_form_for

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