无法在Select2中设置多个值 [英] Can't set multiple values in Select2
问题描述
我试图在select2-jquery组件中显示选定的值。
I am trying to show selected values in select2-jquery component.
var select = $(".select2").select2({
multiple: true,
placeholder: "",
width:'100%',
data: z
});
var selectedValues = $("#sourceValues").val().split(',');
$.each( selectedValues, function(k,v){
$(".select2").select2('val',v);
})
元素 sourceValues
保存值例如:2,4
和 z
是一个对象数组,它按照建议保存id和text。
我可以看到链接到Select2元素的< options>
,但我无法在元素上显示所选值。
此外,如果我尝试在Chrome控制台上运行查询,那么如果我写了类似的话,它就可以工作;
Element sourceValues
holds the value e.g : 2,4
And z
is array of object that holds id and text as suggested.
I can see the <options>
that is linked to the Select2 element but I am unable to show the selected values on the element.
Also, If I try to run the query on Chrome console it works , if I write something like ;
$(".select2").select2('val',4)
因此,<$ c已选择ID为4的$ c>< option> 。
推荐答案
http://ivaynberg.github.io/select2/#documentation
val
附加到选择 - 多值 - 应该选择的
选项的值属性数组。 null为空。
Attached to select - Multi-Valued - Array of the value attributes of the options that should be selected. null for empty.
所以:
var selectedValues = $("#sourceValues").val().split(',');
$(".select2").select2('val',selectedValues);
这篇关于无法在Select2中设置多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!