与此FIDDLE类似,您将如何在加载时显示多个选定值?
<select multiple>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
$('select').select2().select2('val','3')
需要显示类似于此ON LOAD的内容
答案 0 :(得分:7)
使select为多个,然后将值作为数组传递
$('select').select2().select2('val', ['3', '1'])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.3.0/select2.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.3.0/select2.js"></script>
<select multiple="multiple" style="width:300px">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
演示:Fiddle
4.1之后
$('select').select2().val(['3', '2']).trigger('change')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet" />
<select multiple="multiple" style="width:300px">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
演示:Fiddle
答案 1 :(得分:1)
试试下面的方法,它对我有用。
var 数组 = ['3', '1'];
$('#your select id').val(array).trigger("change");