如何在rails的options_from_collection_for_select中选择多个值?目前,我正在选择一个值。我需要选择多个值。
<select id="addr" class="form-control" tabindex="2" onchange="getSelectedDeviceType()" >
<option value="">Select Device</option>
<%= options_from_collection_for_select(@devices,:mac_address, :mac_address,:multiple => true) %>
</select>
答案 0 :(得分:0)
对于多项选择,请尝试
<select multiple>
<option>1</option>
<option>2</option>
</select>
您可以参考this链接以供参考
您可以使用此js函数
function getSelectedOptions(sel) {
var opts = [],
opt;
var len = sel.options.length;
for (var i = 0; i < len; i++) {
opt = sel.options[i];
if (opt.selected) {
opts.push(opt);
}
}
return opts;
}
答案 1 :(得分:0)
通过方法,您可以尝试使用表单f对象:
<%= f.select :addr, options_from_collection_for_select(@devices,:mac_address, :mac_address,:multiple => true), {prompt: 'Please select ...' }, { class: 'form-control', :onchange => "getSelectedDeviceType()" } %>
或带有select_tag:
<%= select_tag 'addr', options_for_select(@devices.map { |s| [s.name, s.id] }), {multiple: true, :onchange => "getSelectedDeviceType()" %>