如何打字时过滤的jQuery自动完成数据 [英] How to filter your jquery autocomplete data while typing
问题描述
到目前为止,我有以下
var aCleanData = ['aaa','aab','faa','fff','ffb','fgh','mmm','maa'];
$('#my-input').autocomplete({
source:aCleanData,
minLength:2
});
目前,如果你键入 AA
, AAA,AAB,美国联邦航空局,MAA
将显示。
Currently if you type aa
, aaa,aab,faa,maa
will show.
我想要做的是当用户键入 FF
所显示将是数据的 FFF,FFB
数据。
What I would like to do is when the user types ff
the data that is shown will be the fff,ffb
data.
基本上,只有什么类型应该从第一个字符开始进行匹配。
Basically, only what is typed should be matched from the first character onwards.
这应该是递归的。当用户键入 FG
, FFF,FFB
应disapear只有 FGH
应该会出现。
This should be recursive. When user types fg
, fff,ffb
should disapear and only fgh
should appear.
在此先感谢您的帮助。
更新:
P.S。看看我的意思是:
p.s. See what I mean here:
http://jqueryui.com/demos/autocomplete/#default
键入 SC
,你会看到的不仅仅是数据多与SC开始。
Type sc
and you'll see more than just the data beginning with sc.
推荐答案
一个可能的解决方案只得到的结果与输入值开始由自己来检查搜索前的数组元素:
One possible solution to get only results starting with the input value is to check the array elements before search by yourself:
var aCleanData = ['aaa','aab','faa','fff','ffb','fgh','mmm','maa'];
$('#my-input').autocomplete({
source: aCleanData,
minLength: 2,
search: function(oEvent, oUi) {
// get current input value
var sValue = $(oEvent.target).val();
// init new search array
var aSearch = [];
// for each element in the main array ...
$(aCleanData).each(function(iIndex, sElement) {
// ... if element starts with input value ...
if (sElement.substr(0, sValue.length) == sValue) {
// ... add element
aSearch.push(sElement);
}
});
// change search array
$(this).autocomplete('option', 'source', aSearch);
}
});
另请参见我的的jsfiddle 。
这篇关于如何打字时过滤的jQuery自动完成数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!