我正在使用jquery组合框,并依靠那里提供的源代码。我需要从远程源提供的数据,所以我用ajax把它连接起来。但是,由于AJAX是一个异步调用,我需要用搜索结果数据更新组合框。如何在jquery自动完成组合框中触发搜索?
如何触发组合框的搜索?
段:
代码/* In
$.widget("ui.combobox", {
_create: function() {
// input definition
*/
.autocomplete({
delay: 0,
minLength: 0,
source: function(request, response) {
// implements retrieving and filtering data from the select
var term = request.term;
if(term.length >= 2){
var abbreviation = term.substring(0,2);
if(!(abbreviation in cache)){
searchResultData = searchCities(abbreviation);
updateOptions(select, searchResultData);
cache[abbreviation] = 1;
}
}
// updates the search widget with options matching request.term
var responseData = filterOptionsForResponse(select, term);
response(responseData);
},
休息是作为设置在jquery的站点。 当searchCities(abbreviation)
返回本地对象数组时,上面的代码工作绝对正常。
searchCities(abbreviation)
:返回匹配缩写updateOptions(select, data)
城市的数组:更新选择组合框中的选项与给定的数据filterOptionsForResponse(select, term)
:更新基于正则表达式
我使用ajax搜索的当前版本:
function searchCities(abbreviation){
if(!!abbreviation){
$.ajax({
url: "/wah/destinationsJson.action",
dataType: "json",
data: {
term: abbreviation
},
type: "GET",
success: function(data){
updateOptions($("#searchbox"), data.cities);
// $("#searchbox input").trigger('autocompleteselect', data.term);
// return data.cities;
}
});
}
}
上面的触发器不会触发搜索!有人可以请解释我怎样才能触发此组合框搜索。 PS:我很抱歉没有提供jsfiddle/jsbin,因为我无法将它渲染到那里。