【问题标题】:Rails 3.2 - Ajax call when text field changesRails 3.2 - 文本字段更改时的 Ajax 调用
【发布时间】:2013-06-20 14:28:47
【问题描述】:

我有一个 Rails 3.2.13 应用程序,在 formslinks 上有一些 Ajax,使用 remote 参数。 问题是我在文档中找不到如何对 文本字段 执行相同操作 - remote 参数不起作用,所以我认为输入对象不支持它。

我想在我的 text_field 对象上绑定 'ajax:xxx' 事件(如 'ajax:success')。 我什至可以使用 UJS 吗?如果没有,我的选择是什么?

这里有一些代码:

<%= form_for @post, :html => {:class => 'form-horizontal'} do |f| %>    
<div class = 'control-group'>
    <%= f.label :title, :html => {:class => 'control-label'} %>     
    <%= f.text_field :title, :placeholder => 'Title', :class => 'input-xxlarge' %>
</div>

<div class = 'control-group'>
    <%= f.label :body, :html => {:class => 'control-label'} %>      
    <%= f.text_area :body, :placeholder => 'Your post here', :class => 'input-xxlarge', 
    :rows => 15 %>
</div>

<div class = 'control-group'>
    <%= f.label :tags, :html => {:class => 'control-label'} %>      
    <%= f.text_field :tags, :placeholder => 'Tags separeted by ,', :class => 'input-xxlarge', 
    :value => '' %>
</div>

<%= f.submit 'Create', :class => 'btn btn-primary'%>

谢谢!

【问题讨论】:

    标签: javascript jquery ajax ruby-on-rails-3 ujs


    【解决方案1】:

    我会将changeblur 事件绑定到input,然后在您的javascript/coffecript 文件上手动调用Ajax

    posts.js

    $('#post_title').change(function() {
      // Do your stuff, instantiate variables, etc...
      $.ajax({
        type: post_or_get,
        url: your_url,
        data: your_data,
        success: function(data) {
          // Handle stuff after hitting the server here
        },
        error: function(data) {
        }
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      相关资源
      最近更新 更多