Question

What I want to do is ask user to choose a combo type (e.g. 3 items combo or 2 items combo) first and then ask them to pick up items in the second select. Thus the second select is a multiple select and allow user to select 2 or 3 items.

I am using select2 with option multiple. But it didn't work. It says Option 'multiple' is not allowed for Select2 when attached to a element. Can anyone help me make this work with 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 %>
Était-ce utile?

La solution

multiple cannot be applied to a select element, you must use a hidden element.

<input type="hidden" id="question" name="question" style="width: 70%;">

Autres conseils

@Diego's answer didn't helped me but this one that has my case may help someone with the same problem:

How to implement multi select in a independent table in Rails?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top