选择2,如果没有可用的结果,则选择文本

Select2, select a text if no results available

本文关键字:选择 结果 文本 如果没有      更新时间:2023-09-26

我使用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        
});

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

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

在初始化选择框时尝试设置标记:true,这将临时将键入的值添加到列表