我正在尝试使用 jQuery 的自动完成插件(这个http://docs.jquery.com/Plugins/Autocomplete)。我的服务器正在返回 JSON 字符串,我试图通过 AutoComplete 插件的 ' parse
' 和 ' formatItem
' 参数在客户端上处理它,如下所示:
$(document).ready(function()
{
$('.searchBox input.textbox').autocomplete('/DoSearch.aspx',
{
mustMatch: false,
autoFill: true,
minChars: 1,
dataType: 'json',
parse: function(data)
{
var array = new Array();
for (var i = 0; i < data.length; i++)
{
array[array.length] = { data: data[i], value: data[i].ID, result: data[i].ID };
}
return array;
},
formatItem: function(row, i, n)
{
return row.ID + ': ' + row.Title;
}
});
});
当我运行它时,我在 Firebug 中得到一个“ sValue.substring is not a function ”错误。但是,如果我在 formatItem 和 parse 函数上设置断点,它们会按预期命中并包含似乎有效的数据。
这是从服务器返回的 JSON 文本的精确复制“n”粘贴:
[{"ID":140177,"Title":"Food Handling","Code":"J01.576.423.200"},{"ID":140178,"Title":"Food Handling","Code":"J01.576.423.200"},{"ID":140179,"Title":"Brain Infarction","Code":"C10.228.140.300.301.200"},{"ID":140180,"Title":"Cerebral Hemorrhage","Code":"C10.228.140.300.535.200"},{"ID":140182,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140183,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140184,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140186,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140188,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140189,"Title":"Sulfonylurea Compounds","Code":"D02.886.590.795"}]
请帮忙,我已经搜索了谷歌和 StackOverflow 寻求帮助,但找不到其他人有这个错误,干杯!