【发布时间】:2017-03-10 20:42:17
【问题描述】:
我目前有一个包含建筑物的下拉菜单,它过滤了一个多选框,其中包含按各自建筑物分组的各种房间。
一切都很好,除了当我通过下拉列表过滤多选框时,多选框正确过滤,但删除了告诉用户构建房间属于什么的 optgroup 标签。
EX:假设我通过下拉菜单选择了会计库
<optgroup label="Accounting Library">
<option value="143">105A</option>
<option value="144">105B</option>
</optgroup>
<optgroup label="Ahmanson Center">
<option value="721">fad</option>
<option value="737">zzz</option>
</optgroup>
然后我得到...
<option value="143">105A</option>
<option value="144">105B</option>
这是正确的,除了我想保留 optgroup 标签以用于 UI 目的。
有谁知道我如何使用我的 javascript 做到这一点?
JAVASCRIPT
$(function() {
var rooms = $('#key_room_ids').html();
$("#key_building_name").on("change",function() {
var building, options;
building = $('#key_building_name :selected').text();
options = $(rooms).filter("optgroup[label='" + building + "']").html();
if (options) {
###This doesn't return the values with the optgroup label
return $('#key_room_ids').html(options);
} else {
return $('#key_room_ids').empty();
}
});
});
表格
<%= simple_form_for(@key, html: { 'data-parsley-validate' => '' }) do |f| %>
###Dropdown Filter
<%= f.collection_select :building_name, Building.order('name ASC'), :id, :name, {:include_blank => '- Please Select A Building To Filter The List Below -'}, { class: "form-control" } %>
###Multi Select Box Grouped by Building
<%= f.grouped_collection_select :room_ids, Building.order('name ASC'), :rooms, :name, :id, :name, {include_blank: "- Please Select The Rooms This Key Works For -"}, {multiple: true, size: 10, :class => "form-control"} %>
<% end %>
【问题讨论】:
标签: javascript jquery ruby-on-rails ruby simple-form