我有這個代碼用於添加自動完成到輸入。工作正常,但用戶也應該能夠在標題中進行搜索(又名「地區」)。jQuery UI自動完成 - 同時在標題中搜索
$(document).ready(function() {
"use strict";
//Autocomplete
$(function() {
$.widget("custom.catcomplete", $.ui.autocomplete, {
_create: function() {
this._super();
this.widget().menu("option", "items", "> :not(.ui-autocomplete-area)");
},
_renderMenu: function(ul,items) {
var that = this,
currentarea = "";
$.each(items, function(index,item) {
var li;
if (item.area !== currentarea) {
ul.append("<li class='ui-autocomplete-area' aria-label='"+item.area+"'>" + item.area + "</li>");
currentarea = item.area;
}
li = that._renderItemData(ul,item);
if (item.area) {
li.attr("aria-label", item.area + ":" + item.label);
}
});
}
});
var data = [
//Title : Value
{ area: "Something", label: "ST A" },
{ area: "Something", label: "ST B" },
{ area: "Other thing", label: "OT A" },
{ area: "Other thing", label: "OT B" },
{ area: "This thing", label: "TT A" }
];
$("#the_things").catcomplete({
delay: 0,
source: data
});
});
});
因此,如果用戶寫道「Something」應該出現標題和相關的值。現在只搜索數值(「標籤」)。
MCVE:https://jsfiddle.net/p91w9y1o/
我需要使它在這兩個地方搜到呢?
你想只搜索「area」還是兩者?你試過什麼了? – Twisty
嗨@twisty,我需要在兩者中進行搜索。我試過的是在MCVE中。 –