选择2显示当前搜索的价值

时间:2016-10-20 10:55:06

标签: javascript jquery twitter-bootstrap select2

我使用Selec2框架通过ajax调用来完成此操作,我在我的项目中成功实现了这一点,但为什么在输入值时搜索它会将当前搜索值显示为可选择的选项,即使没有结果,我也不想隐藏它。有什么建议吗?感谢

显示有效的搜索值,并且可以选择: enter image description here

即使没有结果: enter image description here

HTML:

<select style='width:100%;' id='select-employee'></select>

JS:

$("#select-employee").select2({
    theme: "bootstrap",
    placeholder: "Select employee...",
    minimumInputLength: 1,
    tags: [],
    ajax: {
        url: "getEmployee.php",
        dataType: "json",
        type: "POST",
        delay: 250,
        data: function(params) {
            return {
                term: params.term,
                page: params.page
            };
        },
        processResults: function(data, params) {
            params.page = params.page || 0;
            return {
                results: data.items,
                pagination: {
                    more: (data.page == "1" ? true : false)
                }
            };
        },
        cache: true
    }
});

1 个答案:

答案 0 :(得分:2)

嗨,因为您必须将代码填充为false,尝试将[]更改为false

$("#select-employee").select2({
  theme: "bootstrap",
  placeholder: "Select employee...",
  minimumInputLength: 1,
  tags: false,
  ajax: {
    url: "getEmployee.php",
    dataType: "json",
    type: "POST",
    delay: 250,
    data: function(params) {
        return {
            term: params.term,
            page: params.page
        };
    },
    processResults: function(data, params) {
        params.page = params.page || 0;
        return {
            results: data.items,
            pagination: {
                more: (data.page == "1" ? true : false)
            }
        };
    },
    cache: true
  }
});