select2 ajax 设置默认选中 selected

select2 设置默认选中

在数据源已经存在的情况

$('#mySelect2').val('1'); // Select the option with a value of '1'
$('#mySelect2').trigger('change'); // Notify any JS components that the value changed

在数据源是ajax 的情况下

var newOption = new Option(data.text, data.id, true, true);
$('#companyId').append(newOption).trigger('change');

详情请看官方文档
https://select2.org/programmatic-control/add-select-clear-items
posted @ 2022-08-25 21:31  helloPHPworld  阅读(823)  评论(0)    收藏  举报