【问题标题】:Not able to select the values of jQuery Autocomplete?无法选择 jQuery Autocomplete 的值?
【发布时间】:2016-06-29 10:20:27
【问题描述】:

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

下面是我的代码:

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

【问题讨论】:

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


【解决方案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>

【讨论】:

  • 砰!谢谢:)
猜你喜欢
  • 2010-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多