我想用jQuery插件包装标准的jQuery UI自动完成,以实现多值自动完成。一切都很好,除了一件小事。我想展示关注焦点的建议。我该如何修改我的代码?
(function ($) {
$.fn.myautocomplete = function (options) {
var defaults = {
source: null,
minLength: 0,
separator: ", "
};
var o = {};
$.extend(o, defaults, options);
var split = function(val) { return val.split(/,\s*/); };
var extractLast = function(term) { return split(term).pop(); };
if (o.source == null) return this;
return this.each(function () {
$(this).autocomplete({
source: function(request, response) {
$.ajax({
url: o.source,
data: { term: extractLast(request.term) },
success: function(data) {
response($.map(data, function(item) {
return { label: item.Name };
}));
}
});
},
search: function() {
return extractLast(this.value).length > o.minLength;
},
focus: function() {
//$(this).trigger('keydown.autocomplete');
$(this).data("autocomplete").search($(this).val());
//return false;
},
select: function(event, ui) {
var terms = split(this.value);
terms.pop();
terms.push(ui.item.label);
terms.push("");
this.value = terms.join(o.separator);
return false;
}
});
});
};
})(jQuery);
我已经尝试了你在焦点处理程序中看到的所有内容。
答案 0 :(得分:0)
这对我有所帮助:
var showSuggestions = function() {
var searchString = extractLast(this.value);
if (searchString.trim().length == 0) {
searchString = "[GetAll]";
}
$(this).autocomplete("search", searchString);
};
$(this).focus(showSuggestions).click(showSuggestions);
在服务器上我正在检查它是否是[GetAll]我认为这是空字符串