【问题标题】:Getting a Select Value with Javascript From a Rails Wildcard named select list使用 Javascript 从名为选择列表的 Rails 通配符获取选择值
【发布时间】:2011-07-21 21:41:07
【问题描述】:

所以我需要调用一个选择下拉列表的值,其中我将选择列表命名为通配符值(这是一个通配符值,因为我为用户的每次迭代创建了多个下拉列表,其中包含特定的属性。

= select_tag "#{user.id}", options_for_select([["User", "0"], ["Admin", "1"]]),
    :onchange => "setTimeout('admin_change(#{user.id});', 300);

所以,然后在顶部的 javascript 块中我有这个

function admin_change(id) {
  change_value = $(#id).val();

在此之后,我根据拉取的 change_value 执行不同的 $.AJAX 命令。

我有办法执行此操作吗?

【问题讨论】:

  • 那里有什么不适合你的?
  • $(#id) 与函数的 id 不对应。

标签: javascript jquery ruby-on-rails ajax wildcard


【解决方案1】:

给你需要操作一个类的所有选择,然后:

$('document').ready(function(){
    $('.myselects').change(function(){
       var user_id = $(this).val(); 
       /* 
       if value is not your user id, just add it to options as data-* attribute 
       (ex: data-user-id="123"), and select it using:
       */
       var user_id = $(this).find('option:selected').attr('data-user-id');

       // and, at this point, do anything you need: ajax calls, etc...
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 2011-08-18
    • 2011-05-21
    • 2014-02-09
    • 2022-11-24
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多