两个依赖于Rails模型中数组的下拉菜单项
Two dependent drop down menu items from arrays in model in Rails
我搜索了/stackoverflow几个小时,但没有找到任何符合我要求的东西。我是rails的新手,我正在rails 4.2.4中的bug跟踪系统上工作。这里我有一个名为Bug的类。关于bug的创建,我有两个下拉菜单,将用来自model的数组值填充。我的模型是这样的:
class Bug < ActiveRecord::Base
belongs_to :creator, :class_name => "User", :foreign_key => "creator_id"
belongs_to :developer, :class_name => "User", :foreign_key => "developer_id"
TYPE = ['Feature', 'Bug']
FEATURE_STATUS = ['New', 'Started', 'Completed']
BUG_STATUS = ['New', 'Started', 'Resolved']
validates :title, :presence => true, :uniqueness => true
validates :status, :type, :presence => true
has_attached_file :screen_shot
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
有一些代码来自_form.html。Erb文件,它将显示下拉选项:
...
...
...
<div class="form-group">
<%#= f.label :bug_type, class: "col-sm-2 control-label" %>
<%= f.label :bug_type, t('.bug_type') + ":" %>
<%= f.select :bug_type,
Bug::TYPE,
:prompt => "Select bug type:"
%>
</div>
<div class="form-group">
<%#= f.label :status, class: "col-sm-2 control-label" %>
<%= f.label :status, t('.status') + ":" %>
<%= f.select :status,
Bug::FEATURE_STATUS,
:prompt => "Select status:"
%>
</div>
...
...
...
当前,我的代码在第一个下拉列表中显示数组Bug::TYPE的值,在第二个下拉列表中显示Bug::FEATURE_STATUS的值。我的问题是如何填充第二个下拉与数组值取决于第一个下拉值?也就是说,如果用户从第一个下拉列表中选择了一个值'Feature',那么第二个下拉列表应该显示FEATURE_STATUS数组的值。如果用户从第一个下拉列表中选择了一个值'Bug',那么第二个下拉列表应该显示数组FEATURE_BUG的值。
我是新手,这是我在这里问的第二个问题。任何帮助都将非常感激。
你必须使用JQuery才能做到这一点:
$('#select_id').on('change', function() {
//populate the second one here
});
,然后查看这里了解如何填充select:
的更多细节如何更改
相关文章:
- 如何在映射数组中添加换行符
- javascript结合了数组和字典
- 需要帮助设置json数组
- 不能从angular2中的子组件指定父组件中的数组
- 使用JS将数组转换为json对象
- 数组在递归方法中设置为null
- knockoutjs可观察数组
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 将数组从PHP传递到Javascript
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 在函数中添加数组元素的数值
- 无法通过数组映射绑定
- javascript中的数组出错
- 如何使用 node.js 比较两个 json 数组
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 根据id将json数组组合为一个json数组
- 如何通过数组更新角度子范围
- 隐藏/显示用户列组的菜单项
- 两个依赖于Rails模型中数组的下拉菜单项
- ComboBox.store.loadData不能加载单项数组