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

时间:2015-10-30 11:45:05

标签: javascript jquery jquery-select2

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

用户可以选择键入的值,但仅限于数据库中存在更多结果。

当数据库中没有可用结果时,有没有办法点击在搜索框中添加输入值?

1 个答案:

答案 0 :(得分:2)

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