我不确定它是否可能,但我想这样做,以便 jquery-ui 自动完成与多个关键字一起工作以获得相同的结果。
这是一个示例,但它相当旧,即使使用较旧的 jquery 文件,我似乎也无法使其工作。我对 jquery 和 javascript 不太熟悉,但我可以设法编辑现有的东西。
这是我目前拥有的(没有对多关键字进行任何调整):
<script type="text/javascript">
$(document).ready(function() {
NewAuto();
});
function NewAuto() {
var products = [
<?php foreach($search__1 as $search) {
echo "{value: '". $search['product_name'] ."'}, ";}?>
];
$("#keyword").autocomplete({
source: function(requestObj, responseFunc) {
var matchArry = products.slice(); // Copy the array
var srchTerms = $.trim(requestObj.term).split(/\s+/);
// For each search term, remove non-matches.
$.each(srchTerms, function(J, term) {
var regX = new RegExp(term, "i");
matchArry = $.map(matchArry, function(item) {
return regX.test(item) ? item : null;
});
});
// Return the match results.
responseFunc(matchArry);
},
open: function(event, ui) {
// This function provides no hooks to the results list, so we have to trust the selector, for now.
var resultsList = $("ul.ui-autocomplete > li.ui-menu-item > a");
var srchTerm = $.trim($("#keyword").val()).split(/\s+/).join('|');
// Loop through the results list and highlight the terms.
resultsList.each(function() {
var jThis = $(this);
var regX = new RegExp('(' + srchTerm + ')', "ig");
var oldTxt = jThis.text();
jThis.html(oldTxt.replace(regX, '<span class="srchHilite">$1</span>'));
});
}
});
}
</script>