这是我的jquery代码
$('。typeahead',this).autocomplete({
source: function(request, response) {
$.ajax({
url: 'includes/stations.json',
dataType: 'json',
data: request,
success: function(data) {
response($.map(data, function(item, i) {
return {
label: item.name,
value: item.code
}
}));
},
});
},
minLength: 3
});
我的Json文件是 [ { “code”:“9BP3”, “名字”:“9Bp No3” }, { “代码”:“AA”, “名字”:“Ataria” },{ “代码”:“BILA”, “名字”:“Bheslana” }, { “代码”:“BILD”, “名字”:“比尔迪” },{ “代码”:“HRI”, “名字”:“Hardoi” }, { “代码”:“HRM”, “名字”:“Hadmadiya” } ]
当我输入任意三个字母时,它返回整个json文件值
答案 0 :(得分:0)
q:request.term 这是来自stations.json的过滤器返回值的字符串。您必须传递这样的变量来过滤结果。 stations.json必须是动态生成的文件,如带有json头的php。 q是$ _GET参数,必须进行解析。尝试更改代码:
$.ajax({
url: "includes/stations.json",
dataType: "json",
data: {
q: request.term // here is the string for filter returned values from stations.json. You must pass variable like this to filter results. stations.json must be dynamically generated file like php with json header. q is $_GET parameter and must be parsed.
},
success: function(data) {
response(
$.map(data, function(item, i) {
return {
label: item.name,
value: item.code
}
})
);
}
});
},
minLength: 3,