[英]jQuery Autocomplete Substring Matching
I'm attempting to do jQuery autocomplete such that I can search for multiple words. 我试图做jQuery自动完成,以便我可以搜索多个单词。
For example, if I have smart
, very smart
, and super smart
in a list I'd like to be able to start typing smar
and have all three options show up. 例如,如果我在列表中有smart
, very smart
和super smart
,我希望能够开始键入smar
并显示所有三个选项。
This code will work in the sense that if I start typing from the very beginning like over
it will suggest over smart
which is correct. 此代码将在这个意义上的工作,如果我开始从一开始就喜欢打字over
它会提示over smart
这是正确的。 But it wouldn't suggest it if I type just smart
which is the desired output. 但是,如果我只键入smart
这是所需的输出),则不会建议。
Any idea how I can adjust the code so that I could search and suggest say a substring within the list? 知道如何调整代码,以便可以搜索并建议在列表中说一个子字符串吗?
http://jsfiddle.net/UKgD6/390/ http://jsfiddle.net/UKgD6/390/
var acList = ['smart',
'over smart',
'smart land',
'under smart',
'very smart'
];
$('#ac').autocomplete({
source: function( request, response ) {
var matches = $.map( acList, function(acItem) {
if ( acItem.toUpperCase().indexOf(request.term.toUpperCase()) === 0 ) {
return acItem;
}
});
response(matches);
}
});
Your issue can be fixed by changing the indexOf()
check. 您可以通过更改indexOf()
检查来解决您的问题。 Change === 0
to !== -1
. 将=== 0
更改为!== -1
。 This will return anything that matches, no matter what the index of the search string is within the actual string. 无论搜索字符串的索引在实际字符串内是什么,这都将返回匹配的任何内容。
http://jsfiddle.net/UKgD6/391/ http://jsfiddle.net/UKgD6/391/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.