這個問題說明了一切。我的jquery自動完成從我自己的apis中獲取其源。如果找不到任何匹配項,則只會返回「找不到匹配項」,然後顯示在下拉菜單中。當用戶專注於此時,框中的文本變爲「找不到匹配」。我想要做的是在下拉列表中顯示「找不到匹配」,但是使其不可聚焦/可選。這是我的代碼,截至目前:jquery自動完成,如果沒有找到匹配顯示「找不到匹配項」,但不允許關注它
$(document).ready(function() {
$("input#id_address").autocomplete({
delay: 300,
minLength: 3,
source: function(request, response) {
$.getJSON("/pin/cache/", {q:encodeURI(request.term)}, function(results) {
res = results.results;
response($.map(res, function(item) {
return {
label: item.address,
value: item.address,
lat: parseFloat(item.lat),
lng: parseFloat(item.lng)
}
}));
});
},
select: function(event, ui) {
if (ui.item.value == 'no matches found') {
return;
}
else {
$("#id_address").val(ui.item.value);
var pos = new google.maps.LatLng(ui.item.lat, ui.item.lng);
map.setCenter(pos);
map.setZoom(14);
placeMarker(pos);
}
},
});
});
正如你所看到的,我使用的if else循環來檢查選擇「未找到匹配」:功能沒有被選擇的發現場比賽,什麼也不做。然而,它仍然填充了焦點上的文本「找不到匹配」。我希望在找到匹配項時填充焦點文本的默認功能,但是如果找不到匹配項,則下拉應該是不可焦點的。有什麼建議麼 ?
謝謝,這工作完美。 – tapan 2011-05-07 11:57:21