自动完成功能从数组中获取所有数据,而不仅仅是包含键入字母的数据


var cache = {};
$( "#skills" ).autocomplete({
    minLength: 3,
    source: function( request, response ) {
        var term = request.term;
        if ( term in cache ) {
            response( cache[ term ] );
            return;
        }
        $.getJSON( "/profile/skills-list", request, function( data, status, xhr ) {
            cache[ term ] = data;
            response( data );
        });
    }
});

使用这段代码(来自jQueryUIAutocomplete的示例(,我从数组中获取所有数据,而不仅仅是包含我在输入字段中键入的字母的数据。当我在jQuery网站上尝试这个例子时,它工作得很好。

为什么会发生这种情况?我想念什么?

给出的代码似乎很好。你确定关键字筛选在服务器端工作正常吗?可能是服务器端代码没有根据输入字段中的关键字过滤结果。

var cache = {};
$( "#skills" ).autocomplete({
    minLength: 2,
    source: function( request, response ) {
        var term = request.term;
        $.getJSON( "/profile/skills-list", function( data, status, xhr ) {
            var skills = [];
            $.each(data, function(i, item) {
                if (data[i].indexOf(term) != -1)
                {
                    skills.push(data[i])
                }
            });
            response( skills );
        });
    }
});

罗米是对的。我从服务器上获取所有数据,而服务器端不存在检查值的功能。因此,我需要在JavaScript中检查给定数组的每个值中的字符串位置。。

相关内容

最新更新