我有下面的代码-
$(function() {
var fruits = [
{ value: 'Apple',id: '123', data: 'Apple' },
{ value: 'Pear', id: '543', data: 'Pear' },
{ value: 'Carrot', id: '123', data: 'Carrot' },
{ value: 'Cherry', id: '234', data: 'Cherry' },
{ value: 'Banana', id: '543', data: 'Banana' },
{ value: 'Radish', id: '3423', data: 'Radish' }
];
$("#autocomplete").autocomplete({
lookup: fruits,
onSelect: function (suggestion) {
alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
},
});
});
我想同时基于“值”和“id”进行搜索。有lookupFilter函数,但我不知道如何使用它。这是原始脚本-https://www.devbridge.com/sourcery/components/jquery-autocomplete/
这是一个类似的问题-jQuery autocomplete (devbridge) search from beginning
请帮助!
最佳答案
您可以尝试以下代码:
$(function() {
var fruits = [
{ value: 'Apple',id: '123', data: 'Apple' },
{ value: 'Pear', id: '543', data: 'Pear' },
{ value: 'Carrot', id: '123', data: 'Carrot' },
{ value: 'Cherry', id: '234', data: 'Cherry' },
{ value: 'Banana', id: '543', data: 'Banana' },
{ value: 'Radish', id: '3423', data: 'Radish' }
];
$("#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) < -1 || suggestion.id.toLowerCase().indexOf(queryLowerCase) < -1; //checking with both id as well as value
}
});
});
注意:我无法测试此代码,但我认为它应该对您有用。
关于javascript - jQuery自动完成(devbridge)lookupFilter来搜索多个属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39785291/