【问题标题】:Using Devbridge Autocomplete, is there a way to detect no results?使用 Devbridge Autocomplete,有没有办法检测到没有结果?
【发布时间】:2015-08-26 01:31:44
【问题描述】:

我在输入字段类型文本上使用 Devbridge Autocomplete (https://www.devbridge.com/sourcery/components/jquery-autocomplete) 并从数组中读取结果。

有没有办法:

1) 只允许从结果列表中选择,不能在字段中输入任何其他内容

2) 如果没有找到结果,则触发函数

代码:

var people = ['Ross Chapman', 'Curtis Gomez', 'Dolores Gonzales'];

$('.people').autocomplete({
    lookup: people
});

【问题讨论】:

    标签: javascript jquery ajax forms autocomplete


    【解决方案1】:

    您可以使用 onSearchComplete 方法作为mentioned in the docs here

    onSearchComplete:函数(查询、建议){}

    第二个参数建议是在输入中输入字母时的建议数组。所以通过检查suggestions.length,我们可以捕捉到建议数组为空时的条件,这意味着没有找到结果。

    var people = ['Ross Chapman', 'Curtis Gomez', 'Dolores Gonzales'];
    
    $('.people').autocomplete({
        lookup: people,
        onSearchComplete: function (query, suggestions) {
            if(!suggestions.length){
                console.log('no suggestion');
            }
        }
    });
    

    这是一个演示http://jsfiddle.net/dhirajbodicherla/PSJTQ/21/

    【讨论】:

    • 如何仅在选择建议之后而不是在此之前触发该功能?在此示例中,我只想在从列表中选择某些内容后再次将背景设为白色,而我仍在搜索:jsfiddle.net/murid/PSJTQ/24
    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多