2013-07-14 5 views
2

먼저 사용자에게 조합 유형 (예 : 3 개 항목 조합 또는 2 개 항목 조합)을 선택하고 두 번째 선택 항목에서 항목을 선택하라고합니다. 따라서 두 번째 선택은 다중 선택이며 사용자가 2 개 또는 3 개의 항목을 선택할 수 있습니다.select2 다중 선택이 작동하지 않습니다.

저는 옵션 2와 함께 select2를 사용하고 있습니다. 그러나 그것은 효과가 없었습니다. 옵션 은 요소에 첨부 할 때 Select2에 'multiple'을 사용할 수 없다고 말합니다. 누구나 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 %> 

답변

1

복수를 선택 요소에 적용 할 수 없으므로 숨겨진 요소를 사용해야합니다.

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

감사 디에고. 난 그걸 깨달았 어. 나는 그것이 다른 주제 일 것이라는 것을 알고 있지만, 당신이 그 답을 알고 있는지 궁금해합니다. 지금 select2에서 json 응답을 가져와야합니다. '/dishes/{dish_id}/get_assoc_items.json'에서, dish_id는 이전 select에 의해 채워 져야합니다. 이전 선택에서 선택한 값을 얻으려면 어떻게합니까? – user1495133

+0

그런 다음 어떻게 옵션을 추가합니까? – user12458

관련 문제