Pass array to val in Select2

杀马特。学长 韩版系。学妹 提交于 2019-12-02 00:40:35

问题


I have a problem to set the selected Values of my "Select2". When I pass the id values separated by commas it works. But if the pass like array doesn't work.

this is my Select

<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>     

It works

$("#myselect").select2().select2('val', [1,2,3]);

but this dosn't work

var array_selection = [1,2,3];
$("#myselect").select2().select2('val', [array_selection]);

回答1:


You are putting your array_selection Array into another Array. Change

var array_selection = [1,2,3];
$("#myselect").select2().select2('val', [array_selection]);

to

var array_selection = [1,2,3];
$("#myselect").select2().select2('val', array_selection);


来源:https://stackoverflow.com/questions/33838578/pass-array-to-val-in-select2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!