【问题标题】:How can I use jQuery UJS to submit a form via AJAX after the change event of a select box fires in Rails?在 Rails 中触发选择框的更改事件后,如何使用 jQuery UJS 通过 AJAX 提交表单?
【发布时间】:2010-11-16 20:28:36
【问题描述】:

如何在 Rails 中触发选择框的更改事件后使用 jQuery UJS 提交表单?

我的观点是这样的:

<% for perm in @permissions %>
  <%= form_for [@brand,perm], { :remote => true } do |f| %>
    <tr id="permission_<%= perm.id %>">
      <td><%= perm.configurable.name %></td>
      <td><%= f.select :action, ['none', 'read', 'update', 'manage'] %></td>
    </tr>
  <% end %>
<% end %>

非常简单。我的控制器是这样的:

class PermissionsController < ApplicationController

    before_filter :authenticate_user!

    respond_to :html, :js

    load_and_authorize_resource :brand
    load_and_authorize_resource :permission, :through => :brand

    def index
    end

    def update
      @permission = Permission.find(params[:id])
      @permission.update_attributes(params[:permission])
    end

end

在我的 application.js 中:

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(function() {
  $("#permission_action").change(function() {
    this.form.submit(); // This needs to be an AJAX call, but how?
  });
})

请注意,表单提交可以正常触发。但它正在做一个常规的帖子,而不是一个 AJAX 提交。当我在表单上放置一个提交按钮并使用它时,AJAX 提交工作正常。我需要改变:

this.form.submit();

到 AJAX 调用,但我不知道如何。有人可以帮忙吗?

【问题讨论】:

    标签: ruby-on-rails ajax jquery


    【解决方案1】:

    查看 jquery-ujs 源代码,我认为这可能是最安全的方法:

    // in application.js
    jQuery.fn.ajaxSubmit = function() {
        this.trigger('submit.rails');
    };
    

    它只是像 .submit() 一样触发事件,但具有 ujs 风格,所以你会调用

    $('.my_form').ajaxSubmit();
    

    【讨论】:

      【解决方案2】:

      我看到您已经在使用 jQuery,这很好。以下代码取自这里:http://railscasts.com/episodes/136-jquery

      我强烈建议您熟悉这些屏幕投射,它们对您有很大帮助(轻描淡写)。

      // public/javascripts/application.js
      jQuery.ajaxSetup({ 
        'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
      })
      
      jQuery.fn.submitWithAjax = function() {
        this.submit(function() {
          $.post(this.action, $(this).serialize(), null, "script");
          return false;
        })
        return this;
      };
      
      $(document).ready(function() {
        $("#new_review").submitWithAjax();
      })
      

      【讨论】:

      • 谢谢雨果。我今天实际上观看了这个截屏视频,并尝试了这个例子。为了引用表单,我使用了“this.form.submitWithAjax();”因为我试图从选择框的更改事件中引用表单。我不断收到“submitWithAjax 不是函数”。
      • 我无法尝试此操作,但您能否将 submitWithAjax 定义中的 this.submit(....) 行更改为 this.form.submit(....)。也许你试过 idk。告诉我。
      • 是的,没有任何组合对我有用。这是非常非常令人沮丧的。我终于让它工作了: $("#permission_action").change(function() { $.ajax({ type: "PUT", url: this.form.action, data: "permission[action]=" + $("#permission_action").val() }); 返回 false; });但它只适用于 VERY FIRST LINE!屏幕上的每一行都有一个带有一个名为“permission_action”的选择框的表单。我不知道 jQuery 默认只会绑定到特定名称的第一个元素。
      • 好吧,您对 html 中使用的 id 有一个明显的误解,它们应该是唯一的,如果不是这种情况,请使用类选择器。如果您的文档中有多个表格,您需要能够区分它们;我认为“这个”键会帮助你,但我不能确定你需要什么。做你想做的事应该没有那么难。
      • 将#permission_action 切换为类选择器并使用.permission_action。
      猜你喜欢
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多