如何通过 data-* 属性配置 Select2



根据文档,我应该能够通过选择标签上的数据属性设置一些配置。

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,它正在使用我在初始帖子中包含的代码工作。

供参考:https://github.com/select2/select2/pull/5169

最新更新