无法 select jQuery 自动完成的值?

Not able to select the values of jQuery Autocomplete?

我正在尝试创建自定义的 jQuery UI 自动完成功能,但我在第一步中失败了。当我单击填充的值时,没有任何响应。我无法 select 自动完成的值。谁能告诉我为什么?

下面是我的代码:

HTML:

<!DOCTYPE html>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
  <title>test</title>
<body>
  <div>Select:</div>
  <input id="project" style="width:380px;">
</body>

Javascript:

(function($){

  var $project = $('#project');

  var projects = [
    {
      value: "test1",
      label: "test1",
      desc: "test1",
      icon: "jquery_32x32.png"
    },
    {
      value: "test2",
      label: "test2",
      desc: "test2",
      icon: "jqueryui_32x32.png"
    }
  ];

  $project.autocomplete({
    minLength: 0,
    source: projects,
    focus: function( event, ui ) {
      $project.val(ui.item.value);
      return false;
    }
  }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    var inner_html = '<table><tr><td>' + item.value + '</td></tr></table>';
return $("<li></li>")
                                .data("item.autocomplete", item)
                                .append(inner_html)
                                .appendTo(ul);
  };


})(jQuery);

您只需更换:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>

与:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>

检查以下 fiddle:

(function($){

  var $project = $('#project');

  var projects = [
    {
      value: "test1",
      label: "test1",
      desc: "test1",
      icon: "jquery_32x32.png"
    },
    {
      value: "test2",
      label: "test2",
      desc: "test2",
      icon: "jqueryui_32x32.png"
    }
  ];

  $project.autocomplete({
    minLength: 0,
    source: projects,
    focus: function( event, ui ) {
      $project.val(ui.item.value);
      return false;
    }
  }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    var inner_html = '<table><tr><td>' + item.value + '</td></tr></table>';
return $("<li></li>")
                                .data("item.autocomplete", item)
                                .append(inner_html)
                                .appendTo(ul);
  };


})(jQuery);
<head>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
  <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
    <title>test</title>
 </head>
 <body>
    <div>Select:</div>
    <input id="project" style="width:380px;">
 </body>