Jquery自动完成功能无效

时间:2014-02-22 02:40:11

标签: javascript jquery jquery-ui autocomplete

我从jquery ui demo获得了以下代码。 我做了一些小修改。 这是修改后的代码。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Autocomplete - Custom data and display</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <!--link rel="stylesheet" href="/resources/demos/style.css"-->
  <style>
  #project-label {
    display: block;
    font-weight: bold;
    margin-bottom: 1em;
  }
  #project-description {
    margin: 0;
    padding: 0;
  }
  </style>
  <script>
  $(function() {
    var projects = [
      {
        id: "jquery",
        name: "jQuery",
        location: "the write less, do more, JavaScript library"
      },
      {
        id: "jquery-ui",
        name: "jQuery UI",
        location: "the official user interface library for jQuery"
      },
      {
        id: "sizzlejs",
        name: "Sizzle JS",
        location: "a pure-JavaScript CSS selector engine"
      }
    ];

    $( "#project" ).autocomplete({
      minLength: 0,
      source: projects,
      focus: function( event, ui ) {
        $( "#project" ).val( ui.item.name );
        return false;
      },
      select: function( event, ui ) {
        $( "#project" ).val( ui.item.name );
        $( "#project-id" ).val( ui.item.id );
        $( "#project-description" ).html( ui.item.location );

        return false;
      }
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      return $( "<li>" )
        .append( "<a>" + item.name + "<br>" + item.location + "</a>" )
        .appendTo( ul );
    };
  });
  </script>
</head>
<body>

<div id="project-label">Select a project (type "j" for a start):</div>
<input id="project">
<input type="hidden" id="project-id">
<p id="project-description"></p>


</body>
</html>

现在jquery仅在&#39; j&#39;时弹出自动填充建议。键是按下的。 对于其他按键,它没有做任何事情。 我在这做错了什么?

2 个答案:

答案 0 :(得分:3)

由于默认搜索机制,它会根据字段labelvalue过滤内容。

使用自定义数据,最好自己实现源方法,

$("#project").autocomplete({
    minLength: 0,
    source: function (request, response) {
        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
        var array = $.grep(projects, function (value) {
            return matcher.test(value.id) || matcher.test(value.name) || matcher.test(value.location);
        });
        response(array);
    },
    focus: function (event, ui) {
        $("#project").val(ui.item.name);
        return false;
    },
    select: function (event, ui) {
        $("#project").val(ui.item.name);
        $("#project-id").val(ui.item.id);
        $("#project-description").html(ui.item.location);

        return false;
    }
})
    .data("ui-autocomplete")._renderItem = function (ul, item) {
    return $("<li>")
        .append("<a>" + item.name + "<br>" + item.location + "</a>")
        .appendTo(ul);
};

演示:Fiddle

答案 1 :(得分:1)

您的项目数组必须具有value属性:

var projects = [
        {
            id: "jquery",
            name: "jQuery",
            value:"jQuery",
            location: "the write less, do more, JavaScript library"
        },
        ...
        {
            id: "sizzlejs",
            name: "sizzle JS",
            value:"sizzle JS",
            location: "a pure-JavaScript CSS selector engine"
        }
    ];

这样搜索引擎就可以运行了。