javascript - 如何制作一个搜索功能,搜索产品描述的表格或数组并显示相应的代码

标签 javascript html css autocomplete

我有一个产品列表,我想实现一个搜索功能。
我有一个产品列表和一个与之对应的代码列表。

例如,看下面的代码:

"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",

Image of My Project

当我键入元素的描述时,我希望显示相应的代码。我希望结果显示在产品表的顶部。

例如:如果我输入 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>

关于javascript - 如何制作一个搜索功能,搜索产品描述的表格或数组并显示相应的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54139505/

相关文章:

javascript - 我的脚本只适用于台式机,不适用于移动设备。你能告诉我为什么吗?

javascript - Window.location 可以工作,但不是到处都有

javascript - Doctype 的实际使用

javascript - 动态创建html元素后,是否有办法触发事件?

javascript - 加载 JSON 作为 HTML 中的资源以避免引导 ajax

html - Purecss 路线图和元素状态

javascript - 如何知道触摸何时从一个 div 滑到另一个?

javascript - 在javascript中如何动态获取对象的嵌套属性

python - 在 Django 中发送 html 电子邮件

javascript - 仅使用 Javascript 在滚动条上显示/隐藏元素