jQuery autocomplete (devbridge) search from beginning

时光总嘲笑我的痴心妄想 提交于 2019-12-24 02:42:52

问题


I am using devbridge's autocomplete jQuery script. Is there an option to filter the search from the beginning of the word and not to apply to anywhere in a word?

Like if I would type "R" then I would like to see only the words starting with R and not the R character in every word.

Basicly a search from beginning kind of filter is what I am looking for.

How could this be done? Is there an option for this?

<script>
$(function() {

var fruits = [
   { value: 'Apple',  data: 'Apple' },
   { value: 'Pear',   data: 'Pear' },
   { value: 'Carrot', data: 'Carrot' },
   { value: 'Cherry', data: 'Cherry' },
   { value: 'Banana', data: 'Banana' },
   { value: 'Radish', data: 'Radish' }
];

  $("#autocomplete").autocomplete({
        lookup: fruits,
        onSelect: function (suggestion) {
          alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
        },
  });
});
</script>

回答1:


Add lookupFilter function:

$("#autocomplete").autocomplete({
    lookup: fruits,
    onSelect: function (suggestion) {
      alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
    },
    lookupFilter: function (suggestion, query, queryLowerCase) {
        return suggestion.value.toLowerCase().indexOf(queryLowerCase) === 0;
    }
});


来源:https://stackoverflow.com/questions/27313580/jquery-autocomplete-devbridge-search-from-beginning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!