关于javascript:无法选择jQuery自动完成的值?

Not able to select the values of jQuery Autocomplete?

我正在尝试创建自定义的jQuery UI自动完成功能,但第一步本身却失败了。当我单击填充的值时,没有任何响应。我无法选择自动完成的值。任何人都可以让我知道为什么吗?

下面是我的代码:

HTML:

1
2
3
4
5
6
7
8
<!DOCTYPE html>
<script src="http://code.jquery.com/jquery-1.11.0.min.js">
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js">
  test
<body>
  Select:
  <input id="project" style="width:380px;">
</body>

Javascript:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(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);


您只需要替换:

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

具有:

1
2
3
<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 src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js">

检查以下小提琴:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(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);
1
2
3
4
5
6
7
8
9
10
<head>
\t\t<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
\t\t<script src="http://code.jquery.com/jquery-2.1.4.min.js">
\t\t<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js">
  \t\ttest
\t</head>
\t<body>
  \t\tSelect:
  \t\t<input id="project" style="width:380px;">
\t</body>