select2的清空,当select2有值时,再使用初始化的方式赋值,会在原来的值后追加,需要使用和select一样的清空方式


$('.select2bs4').empty();

ajax的绑定:


$('.sjksy').select2({
            theme: 'bootstrap4',
            ajax: {
                url: '/aa/bbb/type/'+$("input[name='gzlx']:checked").val()+'/',
                processResults: function (data) {
                    // Transforms the top-level key of the response object from 'items' to 'results'
                    return {
                        results: data.data
                    };
                }
            }
        });

json返回值不是默认的json时,需要使用方法processResults处理一下,详见:

https://select2.org/data-sources/ajax