无法选择jQuery自动完成的值?

时间:2016-06-29 10:20:27

标签: javascript jquery jquery-ui jquery-ui-autocomplete

我正在尝试并尝试创建自定义的jQuery UI自动完成功能,但我在第一步中失败了。当我点击填充的值时,没有响应。我无法选择自动完成的值。谁能告诉我原因?

以下是我的代码:

HTML:

(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);

使用Javascript:

    var myarray = ["item 1", "item 2", "item 3", "item 4"];

    //removes the first element of the array, and returns that element apart from item 1.
    myarray.shift(); 
    console.log(myarray); 

1 个答案:

答案 0 :(得分:1)

您只需要替换:

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

检查以下小提琴:

(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>