admin管理员组文章数量:1187660
How do I enable exact match from start of string using jquery autocomplete with input from simple array?
If i have the following in an array:
- smart
- oversmart
- smartland
- undersmart
- verysmart
And if I am typing "sma..." in the text input, i must be shown only smart and smartland, not the others.
How do I enable exact match from start of string using jquery autocomplete with input from simple array?
If i have the following in an array:
- smart
- oversmart
- smartland
- undersmart
- verysmart
And if I am typing "sma..." in the text input, i must be shown only smart and smartland, not the others.
Share Improve this question asked May 5, 2012 at 17:13 VKVKVKVK 1311 gold badge1 silver badge5 bronze badges 1- possible duplicate of jQuery Autocomplete plug-in search configuration – j08691 Commented May 5, 2012 at 17:18
5 Answers
Reset to default 16You just need to modify source parameter as a function to suit your needs. Like this:
http://jsfiddle.net/UKgD6/
Update: Adding code to answer:
var acList = ['smart', 'oversmart', 'smartland', 'undersmart', 'verysmart'];
$('#ac').autocomplete({
source: function (request, response) {
var matches = $.map(acList, function (acItem) {
if (acItem.toUpperCase().indexOf(request.term.toUpperCase()) === 0) {
return acItem;
}
});
response(matches);
}
});
The way to do this is documented at http://api.jqueryui.com/autocomplete/
<script>
var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ];
$( "#autocomplete" ).autocomplete({
source: function( request, response ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( tags, function( item ){
return matcher.test( item );
}) );
}
});
</script>
You can use regular expressions to match the entered substring with the values in the array you have.
JQuery has way too many plug-ins. Just sprinkle a little regEx on it and write something like this (I didn't test it but I've done similar stuff before):
$('#someTextInput').keyup( function(){
var regExMatch = new RegEx( '^' + $(this).val() );
$_LIs = $('#someUl li');
$_LIs.hide();
$_LIs.each( function(){
if( this.innerText.match(regExMatch) ) { $(this).show(); }
} );
} );
You can remove the '^' +
in the new RegEx
parameters to restore the behavior you're describing as problematic. '^'
signifies beginning of a line/string selection so 's' won't match unless it's at the start.
This has changed since the previous answer was accepted. There is now a lookupFilter
option to do this and it is much simpler. From https://stackoverflow.com/a/23239873/1204434, just add this to your list of options:
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
return suggestion.value.toLowerCase().indexOf(queryLowerCase) === 0;
},
版权声明:本文标题:javascript - auto complete exact match from start using jquery autocomplete from simple array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738374717a2083153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论