当给出 1 个参数时(在与搜索字段不同的字段中),如何过滤列表。例如,用户指定组“Test”并开始在#ContactPerson 字段中搜索。我只想要显示 "group":"Test"的记录。
("#ContactPerson").autocomplete({
source: [{"value":"XYZ","name":"ZJL","group":"Test"},...]
...
这是我到目前为止所尝试过的,但它不起作用:http://jsfiddle.net/71puvk59/
最佳答案
根据this answer您可以覆盖搜索功能并应用所有过滤器。
就您而言:
var source = [
{"value": "JL", "name": "JL", "group": "Test"},
{"value": "MV", "name": "MV", "group": "Family"}
];
$('#ContactPerson').autocomplete({
source: source,
search: function (oEvent, oUi) {
// get current input value
var sValue = $(oEvent.target).val();
//groupValue
var groupValue = $("#group").val();
// init new search array
var aSearch = [];
// for each element in the source array ...
$(source).each(function (iIndex, sElement) {
var groupFilterMatch = ((groupValue && sElement.group.indexOf(groupValue) != -1) || !groupValue);
var nameFilterMatch = sElement.name.indexOf(sValue) != -1;
// ... APPLY YOUR FILTERS HERE
//Example: filter group, if it is defined, and filter name
if (groupFilterMatch && nameFilterMatch) {
// add element
aSearch.push(sElement);
}
});
// change search array
$(this).autocomplete('option', 'source', aSearch);
}
});
关于javascript - jQuery 自动完成 : downsize list on value input field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25996377/