草庐IT

javascript - Select2.js v4.0 : how set the default selected value with a local array data source?

coder 2024-07-28 原文

通过使用 select2.js v4插件,当我使用本地数组数据作为源时,如何设置默认选择值?

以这段代码为例

var data_names = [{
  id: 0,
  text: "Henri",
}, {
  id: 1,
  text: "John",
}, {
  id: 2,
  text: "Victor",
}, {
  id: 3,
  text: "Marie",
}];

$('select').select2({
  data: data_names,
});

如何设置id 3为默认选中值?

最佳答案

$('.select').select2({
        data: data_names,
    }).select2("val",3);

关于javascript - Select2.js v4.0 : how set the default selected value with a local array data source?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31654200/

有关javascript - Select2.js v4.0 : how set the default selected value with a local array data source?的更多相关文章

随机推荐