我有一個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。 –