我想要做的是讓用戶先選擇一個組合類型(例如3個物品組合或2個物品組合),然後讓他們在第二個選擇中選取物品。因此第二個選擇是多重選擇並且允許用戶選擇2或3個項目。select2多選不起作用
我正在使用select2與選項倍數。但它沒有奏效。它表示選項連接到元素時,Select2不允許使用「倍數」。任何人都可以幫我做這個工作與select2?
<script type="text/javascript">
$(document).ready(function() {
$('select#box_name').change(function() {
var dish_id = $(this).find('option:selected').val();
$.read(
'/dishes/{dish_id}/get_assoc_items.json',
{ dish_id: dish_id },
function (data) {
var options = ''
$.each(data, function(idx, item) {
options = options + '<option value="'+item.id+'">'+item.name+'</option>'
});
var select = $('select#box_assoc_items')
$('option', select).remove();
select.append(options);
}
);
});
});
$(document).ready(function() {
$('select#box_assoc_items').select2({
placeholder: 'choose items',
multiple: true
});
});
</script>
<%= simple_form_for([@restaurant, @order, @box]) do |f| %>
<%= f.input :name, :collection => @dishes %>
<%= f.input :assoc_items, :collection => [[]], :input_html => {:maxlength => 2, :style => 'width:400px'} %>
<%= f.input :price %>
<%= bootstrap_button(f, "Add a dish") %>
<% end %>
感謝迭戈。我意識到。我知道這將是一個不同的話題,但不知道你是否知道答案。現在我必須在select2中獲取json響應。因爲在'/dish/{dish_id}/get_assoc_items.json'中,dish_id需要由前一個選擇填充。我如何從上一次選擇中獲得選定的值? – user1495133
那我該如何添加選項呢? – user12458