简体   繁体   中英

Select2JS - there is a default value in my list

I use select2js in its last version, but I don't know why, I have a "s" by default in my list when I click in. It uses Ajax call, and I put minimumInputLength to 2 to avoid this. Here is my call :

$('.search-location').select2({
    language: 'fr',
    placeholder: "Ville ou CP",
    minimumInputLength: 2,
    maximumSelectionLength: 4,
    width: '100%',
    ajax: {
        url: ajaxurl,
        dataType: "json",
        data: function (params) {
            var queryParameters = {
                q: params.term,
                action: 'get_the_locations'
            }
            return queryParameters;
        },
        cache: false,           
        processResults: function (data) {
            return {
                results: $.map(data, function (item) {
                    return {
                        text: item.label,
                        id: item.number
                    }
                })
            };
        }
    }
}).on('select2:select', function (e) {
    var data = e.params.data;
});

You can test here : https://www.cabinet-hermes.fr/ on the main search bar, the input called "VILLE, CP ou DEPARTEMENT". Thanks.

If you're using a version higher than V4 you could try adding to your options :

language: { inputTooShort: function () { return ''; } }

That way if the user inputs less than 2 characters nothing will appear.

I obviously don't have enough elements to reproduce your problem, but that's the most general way of removing any unneeded options in select2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM