我有一個jqueryui自動完成輸入,它使用ajax調用填充建議。如何確保表單提交時,不會出現建議?我嘗試過調用「close」方法,但這不起作用,因爲表單可能在ajax調用完成之前提交。
我創建了一個小提琴在這裏展示我的問題: http://jsfiddle.net/P7VJf/5/
這是基於jQueryUI的演示在這裏: http://jqueryui.com/demos/autocomplete/#remote-jsonp
下面是代碼:
$(function(){
$('form').submit(function(){
/*
when the form is submitted, i want to cancel auto complete
if the ajax call hasn't completed, the suggestions
will still show even if we call close here
*/
$("#city").autocomplete("close");
$('span').html(Math.random() * 3);
return false;
});
$("#city").autocomplete({
delay: 300,
source: function(request, response) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function(data) {
response($.map(data.geonames, function(item) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
},
minLength: 2
});
});
任何幫助不勝感激。