我有一个json文件,该文件托管在github中用于测试目的。我的问题是,如何根据用户使用ajax输入的第一个字符获取数据,例如,当我键入a或A时,将显示“Applebees”。自动完成获取第一个输入字符的数据
var searchDevice = (function(){
var $el = $('.form-wrapper');
var $brandName = $el.find('#brandName');
var $brandModel = $el.find('#brandModel');
var $search = $el.find('#search');
var brandName = '';
var brandModel = '';
$brandName.on('keyup', searchType);
$brandModel.on('keyup', searchType);
function searchType(e){
brandName = $brandName.val();
brandModel = $brandModel.val();
brandModel.length > 0 && brandName.length > 0 ? $search.removeAttr("disabled") : $search.attr("disabled","disabled");
if(brandName.length > 0){
$brandName.autocomplete({
minlength: 1,
source: function(request, response) {
$.ajax({
url: "https://ronnelsanchez.github.io/diamond/routers.json",
dataType: "json",
data: {
searchText: request.term
},
success: function (data) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response($.map(data.routers, function (item) {
var text = item.homeTeam;
if (text && (matcher.test(text))) {
return {
label: item.homeTeam,
};
}
}));
},
error: function(data) {
alert("test");
}
}); // end Ajax request
} // end source
})
} // end if
} // end function
})();
This is the json structure:
{
"routers": [{
"point": "new GLatLng(40.266044,-74.718479)",
"homeTeam": "Lawrence Library",
"awayTeam": "LUGip",
"markerImage": "images/red.png",
"information": "Linux users group meets second Wednesday of each month.",
"fixture": "Wednesday 7pm",
"capacity": "",
"previousScore": ""
},
{
"point": "new GLatLng(40.211600,-74.695702)",
"homeTeam": "Hamilton Library",
"awayTeam": "LUGip HW SIG",
"markerImage": "images/white.png",
"information": "Linux users can meet the first Tuesday of the month to work out harward and configuration issues.",
"fixture": "Tuesday 7pm",
"capacity": "",
"tv": ""
},
{
"point": "new GLatLng(40.294535,-74.682012)",
"homeTeam": "Applebees",
"awayTeam": "After LUPip Mtg Spot",
"markerImage": "images/newcastle.png",
"information": "Some of us go there after the main LUGip meeting, drink brews, and talk.",
"fixture": "Wednesday whenever",
"capacity": "2 to 4 pints",
"tv": ""
},
{
"point": "JSPR(0101010101,0101010101)",
"homeTeam": "Jasper Lepardo is the Best",
"awayTeam": "Jasper Lepardo",
"markerImage": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQzxidVfEMBqaJBCgpAVTGsn7k70eZShuRdDeYkTD4je4A_PHtnxfL_bg",
"information": "napaka Pogi ni Jasper",
"fixture": "Pogi mo naman po Jasper",
"capacity": "10 jasper",
"tv": ""
}
]
}
而不是'minlength:1,';你可以试试'minLength:1,'。选项必须区分大小写。 – vijayP
你想要所有的值是自动完成的组件? –
我已将它更改为minLength:1,但是当我键入静态图像时,它会显示所有带有字符的数据。不过感谢vijayP。 –