var selectedValues = new Array();
selectedValues[0] = "1";
selectedValues[1] = "4";
$('#select2-multiple-employees').select2('val', selectedValues);
它仅选择 selectedValues 数组中的第一个索引。 我正在使用select2版本:4.0.8
还有其他方法可以做到这一点吗?这对我不起作用
解决方案: 在插入selectedValues下添加
$('#select2-multiple-employees').trigger('change');
最佳答案
以下是 select2
文档的 2 段摘录:
Configuration: To configure custom options when you initialize Select2, simply pass an object in your call to .select2()
- 但你没有传递这样的对象
Basic Usage: The DOM cannot be safely manipulated until it is "ready". To make sure that your DOM is ready before the browser initializes the Select2 control, wrap your code in a
$(document).ready()
block.
- 但您不等待页面准备就绪
我希望这会给你一些指导。
关于javascript - select2 从 javascript 选择多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58248548/