如果没有可用结果,请选择 2,select 一个文本

Select2, select a text if no results available

我使用 Select2 从数据库中获取下拉结果:

$("#loadingdata").select2({
    placeholder: "3 characters to go",
    allowClear: true,
    minimumInputLength: 3,
    ajax: {
        url: "ajax_cats.php",
        dataType: 'json',
        quietMillis: 100,
        data: function (term, page) { // page is the one-based page number tracked by Select2
        return {
        q: term //search term           
        };
        },
        results: function (data) {
        //var more = (page * 10) < data.total; // whether or not there are more results available

        // notice we return the value of more so Select2 knows if more results can be loaded
        return {results: data};
        }                       
    },  

    // user can select value
    createSearchChoice:function(term, data) {
        if ($(data).filter(function() {
            return this.text.localeCompare(term)===0; }).length===0) {
                return {id:term, text:term};
            }
        },

    dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller        
});

用户可以 select 键入值,但前提是数据库中存在更多结果。

当数据库中没有可用结果时,有没有一种方法可以单击以在搜索框中添加键入的值?

尝试在初始化 select 框时设置 tags:true,这会将键入的值临时添加到列表