[英]Pass array to val in Select2
我在設置“Select2”的選定值時遇到問題。 當我傳遞由逗號分隔的 id 值時,它會起作用。 但是如果像數組這樣的傳遞不起作用。
這是我的選擇
<select class="form-control" id="myselect" multiple="multiple">
<option id="1">Value1</option>
<option id="2">Value2</option>
<option id="3">Value3</option>
<option id="4">Value4</option>
<option id="5">Value5</option>
</select>
有用
$("#myselect").select2().select2('val', [1,2,3]);
但這不起作用
var array_selection = [1,2,3];
$("#myselect").select2().select2('val', [array_selection]);
您將array_selection
Array 放入另一個 Array。 改變
var array_selection = [1,2,3];
$("#myselect").select2().select2('val', [array_selection]);
到
var array_selection = [1,2,3];
$("#myselect").select2().select2('val', array_selection);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.