如何实现" mustMatch"和" selectFirst"在jQuery用户界面自动完成? [英] How to implement "mustMatch" and "selectFirst" in jQuery UI Autocomplete?
问题描述
我最近迁移我的几个自动完成插件由 bassistance 制作了一只< A HREF =http://jqueryui.com/demos/autocomplete/> jQuery用户界面自动完成。
I recently migrated a few of my Autocomplete plugins from the one produced by bassistance to the jQuery UI autocomplete.
如何能在mustMatch和selectFirst只有回调和其他选项可以实现无需修改核心自动完成code本身?
How can the "mustMatch" and "selectFirst" be implemented with just callbacks and other options without modifying the core autocomplete code itself?
推荐答案
我想我解决了这两个功能...
I think I solved both features...
为方便起见,我用了一个世俗的选择:
To make things easier, I used a common custom selector:
$.expr[':'].textEquals = function (a, i, m) {
return $(a).text().match("^" + m[3] + "$");
};
在code的其余部分:
The rest of the code:
$(function () {
$("#tags").autocomplete({
source: '/get_my_data/',
change: function (event, ui) {
//if the value of the textbox does not match a suggestion, clear its value
if ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0) {
$(this).val('');
}
}
}).live('keydown', function (e) {
var keyCode = e.keyCode || e.which;
//if TAB or RETURN is pressed and the text in the textbox does not match a suggestion, set the value of the textbox to the text of the first suggestion
if((keyCode == 9 || keyCode == 13) && ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0)) {
$(this).val($(".ui-autocomplete li:visible:first").text());
}
});
});
如果您的任何自动完成建议包含由正则表达式中使用的任何特殊字符,则必须逃出这些字符米范围内[3]在自定义选择:
If any of your autocomplete suggestions contain any 'special' characters used by regexp, you must escape those characters within m[3] in the custom selector:
function escape_regexp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
和更改自定义选择:
$.expr[':'].textEquals = function (a, i, m) {
return $(a).text().match("^" + escape_regexp(m[3]) + "$");
};
这篇关于如何实现&QUOT; mustMatch&QUOT;和&QUOT; selectFirst&QUOT;在jQuery用户界面自动完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!