javascript - 如何制作搜索产品描述的表格或数组并显示相应代码的搜索功能
问题描述
我有一个产品列表,我想实现一个搜索功能。
我有一个产品列表和与之对应的代码列表。
例如,看下面的代码:
"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>
推荐阅读
- python - 语音 AIY 套件加密错误中的 Cloudspeech 演示
- ubuntu - 如何使用 IntelliJ IDE 将 JavaFX 应用程序部署为 debian 包
- python - 如何链接到 Excel 单元格内 PDF 文档的特定页面?
- java - 如果json属性名称和java对象属性不同,如何将json字符串转换为java对象
- kubernetes - 在 IBM Cloud 中部署没有默认工作程序池的 K8S 集群
- python - 如何替换引号内和不带引号的反斜杠?
- php - 在 Microweber 的引导轮播的 for 循环中显示 3 个帖子项目
- javascript - AG-Grid 是否有像 onDataLoaded 这样的事件,或者有没有其他方法可以做同样的事情?
- python-3.x - Python - 使用类的多线程
- jenkins - 在另一个 jenkinsfile 中运行外部 jenkinsfile