我有一个产品列表,我想实现一个搜索功能。
我有一个产品列表和一个与之对应的代码列表。
例如,请参阅以下代码:
"15L Full Bottle - 1015",
"15L Empty Bottle - 15",
"11L Full Bottle - 1011",
"11L Empty Bottle - 11",
"One Way Bottle - 1030",
"600ml Bottle - 5760",
"5L Cask - 5713",
"10L Cask - 5714",
"10L Piccadilly Cask - 5703",
"1.5L Mt Franklin Still PET - 5849",
"1.5L Neverfail Still PET - 5717",
"Cooler (Monthly Rental) - 97101",
"Cooler (Annual Rental) - 97112",
"Delivery Fee - 97550",
我的项目图片
当我键入项目的描述时,我希望显示相应的代码。我希望结果显示在产品表的顶部。
例如:如果我输入15L
它应该显示:
"15L Full Bottle - 1015"
"15L Empty Bottle - 15"
下面是一个例子。希望这是您正在寻找的那个。
$( function() {
var availableTags = [
"15L Full Bottle - 1015",
"15L Empty Bottle - 15",
"11L Full Bottle - 1011",
"11L Empty Bottle - 11",
"One Way Bottle - 1030",
"600ml Bottle - 5760",
"5L Cask - 5713",
"10L Cask - 5714",
"10L Piccadilly Cask - 5703",
"1.5L Mt Franklin Still PET - 5849",
"1.5L Neverfail Still PET - 5717",
"Cooler (Monthly Rental) - 97101",
"Cooler (Annual Rental) - 97112",
"Delivery Fee - 97550"
];
function split( val ) {
return val.split( /,s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.on( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
} );
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Multiple values</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Search any keyword: </label>
<input id="tags" size="50">
</div>
</body>
</html>