7

链接到小提琴: http: //jsfiddle.net/nEapJ/(工作)

var items = [{
   label : 'a',
   value : 'a',
},{
   label : 'b',
   value : 'b',
},{
   label : 'c',
   value : 'c',
}];

$('input').autocomplete({
    source : items
});​

此代码有效,但是当我想通过回调函数设置源时,它不起作用

链接到小提琴: http: //jsfiddle.net/B3RWj/(不工作)

$('input').autocomplete({
    source : function(request, response){
            response(items);
          }
});​

当我输入“a”时,它会给出 a,b,c 作为结果。

那么,我错过了什么?

提前致谢。

4

3 回答 3

2

在回调函数中,由您来进行过滤。

从文档中提取:

第三种变体,回调,提供了最大的灵活性,可用于将任何数据源连接到自动完成。回调有两个参数:

一个请求对象,具有一个名为“term”的属性,它指的是文本输入中当前的值。例如,当用户在城市字段中输入“new yo”时,自动完成项将等于“new yo”。响应回调,它期望单个参数包含要向用户建议的数据。此数据应根据提供的 term 进行过滤,并且可以采用上述任何简单本地数据的格式(字符串数组或带有标签/值/两个属性的对象数组)。在请求期间提供自定义源回调以处理错误时,这一点很重要。即使遇到错误,您也必须始终调用响应回调。这确保了小部件始终具有正确的状态。

于 2012-09-14T13:10:42.253 回答
2

看代码:

$('input').autocomplete({
    source : function(request, response){
        var term = request.term;
        var result = [];

        //make your code here to filter the item by term. 
        //put them into the result array such as.

        response(result);//this will show in the selection box.
    }
});​
于 2014-05-13T14:38:27.367 回答
0

如果要使用回调函数而不是源数组或字符串,则必须添加 response($.ui.autocomplete.filter(items, request.term));

在您的功能中:

source : function(request, response){}

当您将源定义为数组或字符串时,这就是自动完成功能,但对于回调,您必须添加它。

于 2015-04-26T17:37:21.753 回答