扶手:提交(通过AJAX)时,下拉选项点击 [英] Rails: submit (via AJAX) when drop-down option clicked

查看:194
本文介绍了扶手:提交(通过AJAX)时,下拉选项点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是当选择一个下拉框的选项来自动提交一个AJAX形式最简单,最优雅的方式?我创建一个管理页面,管理员可以修改用户权限(其中权限存储为整数),和我想的权限字段是一个下拉框,自动提交和更新管理员时,点击他希望用户可以选择

下面是什么,我目前正在研究的精简版本。我需要知道将其转换为远程表单时,一个选项被点击自动提交的最佳方式。

随意点出任何切线建议或任何其他......我是比较新的Rails和刚刚到达的地方,我可以用code时不经常引用其他人的工作点。

 <! - 这是指标的说法,顺便说一句。 - >
<%的会员在@ story.memberships%GT;
  <%的form_for会员办| F | %>
    <%= f.select:权限,[['无',0],['管理',9]%GT;
  <%结束%GT;
<%结束%GT;
 

解决方案

有一对夫妇的方式来处理这个问题。当然,观察现场的方式之一,但你也可以使用一个简单的JavaScript或remote_function做到这一点:

下面是一个简单的JS方式:

 <%remote_form_for会员办| F | %>
    <%= f.select:权限,[['无',0],['管理',9] {}:的onchange => this.form.submit()%>
<%结束%GT;
 

另一种方法是这样的事情,并会避开表单生成器(这可能有几个语法错误):

 <%= select_tag(:权限,[['无',0],['管理',9],{:的onchange =>中#{remote_function( :URL => permissions_path(:story_id =>的故事,
             :与=> '权限='+价值)})
 

我个人preFER前者。

What is the easiest and most graceful way to auto-submit an AJAX form when a drop-down box's option is selected? I'm creating an administration page where the admin can modify user permissions (where "permissions" is stored as an integer), and I would like the "permissions" field to be a drop-down box that automatically submits and updates when the administrator clicks on the option he wants the user to have.

Here's the stripped-down version of what I'm currently looking at. I need to know the best way to convert this to a remote form that automatically submits when an option is clicked on.

Feel free to point out any tangential suggestions or anything else...I'm relatively new to Rails, and only just reaching the point where I can write code without constantly referencing others' work.

<!-- This is the "index" view, by the way. -->
<% for membership in @story.memberships %>
  <% form_for membership do |f| %>
    <%= f.select :permissions, [['none', 0], ['admin', 9]] %>
  <% end %>
<% end %>

解决方案

There are a couple ways to handle this. Certainly the observe field approach is one, but you could also use a simple javascript or a remote_function to do it:

Here is the simple JS approach:

<% remote_form_for membership do |f| %>
    <%= f.select :permissions, [['none', 0], ['admin', 9]], {}, :onchange => 'this.form.submit()' %>
<% end %>

The other way would be something like this, and would eschew the form builder (and this may have a couple syntax errors):

<%= select_tag(:permissions, [['none', 0], ['admin', 9]], {:onchange => "#{remote_function(:url  => permissions_path(:story_id => story,
             :with => "'permission='+value")}"})

Personally I prefer the former.

这篇关于扶手:提交(通过AJAX)时,下拉选项点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆