根据文档,我应该能够通过选择标记上的数据属性设置一些配置。
https://select2.org/configuration/data-attributes
在我的代码中,我尝试了以下操作:
<select id="customer_select_form_customer" name="customer_select_form[customer]" class="select2-ajax-enabled form-control" style="width: 500px" data-theme="bootstrap4" data-ajax--url="/customer/ajaxList" data-ajax--cache="true" data-select2-id="customer_select_form_customer" ></select>
我尝试用以下命令初始化它,但它什么也没做:
// enable the customer ajax list
$('.select2-ajax-enabled').select2({});
我还尝试设置参数并覆盖它们,但这也不起作用:
// enable the customer ajax list
$('.select2-ajax-enabled').select2({
theme: 'bootstrap4',
minimumInputLength: 3,
ajax: {
url: '',
dataType: 'json',
cache: 'true'
}
});
为了使数据属性正常工作,我是否缺少配置/设置? 如果我指定整个配置并将其传递给 select2,它会按预期工作。
我正在使用 jQuery v3.3.1 和 Select2 4.0.6-rc.1
最佳答案
跟进这个问题是因为我找到了答案: 由于 v4.0.6 上的错误,这不起作用。我恢复到 v4.0.5,它正在使用我在初始帖子中包含的代码运行。
关于javascript - 如何通过 data-* 属性配置 Select2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49060257/