【问题标题】:selec2 search - Return no results found message if a specific criteria didnt matchselect2 search - 如果特定条件不匹配,则返回未找到结果消息
【发布时间】:2018-01-17 15:20:20
【问题描述】:

我正在使用 select2 4.0.3 进行搜索下拉菜单。据我了解,它的默认功能与下拉菜单的开头不匹配。所以我实现了下面给定的代码

function matchStart(params, data) {
    params.term = params.term || '';
    if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) == 0) {
        return data;
    }
    return false;
}

$("select").select2({
    placeholder : "Input country name or select region",
    matcher : function (params, data) {
        return matchStart(params, data);
    },
});

我的问题是,即使没有找到匹配的结果,下拉菜单也不会显示“未找到结果”消息。谁能帮我解决这个问题。

提前致谢。

【问题讨论】:

    标签: jquery-select2 jquery-select2-4


    【解决方案1】:

    尝试将matchStart 的返回值从false 更改为null

    您还可以删除 matcher 参数周围的额外函数。结果:

    function matchStart(params, data) {
        params.term = params.term || '';
        if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) == 0) {
            return data;
        }
        return null;
    }
    
    $("select").select2({
        placeholder: "Input country name or select region",
        matcher: matchStart
    });
    

    【讨论】:

      猜你喜欢
      • 2014-09-21
      • 1970-01-01
      • 2014-05-21
      • 2021-10-18
      • 2012-05-31
      • 1970-01-01
      • 2020-06-30
      • 2021-04-04
      • 1970-01-01
      相关资源
      最近更新 更多