【问题标题】:Jquery Autocomplete _renderItem does not work when Function is called a second time第二次调用函数时,Jquery Autocomplete _renderItem 不起作用
【发布时间】:2017-08-15 11:49:40
【问题描述】:

我想生成两个带有自动完成功能的输入字段,以生成图标。我使用了_renderItem,但我意识到——通过使用单个类为多个输入字段调用自动完成——第二个字段不会调用_renderItem 函数:

看这里:

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


var projects = [{
    value: "jquery",
    label: "jQuery",
    desc: "the write less, do more, JavaScript library",
    icon: "jquery_32x32.png"
  },
  {
    value: "jquery-ui",
    label: "jQuery UI",
    desc: "the official user interface library for jQuery",
    icon: "jqueryui_32x32.png"
  },
  {
    value: "sizzlejs",
    label: "Sizzle JS",
    desc: "a pure-JavaScript CSS selector engine",
    icon: "sizzlejs_32x32.png"
  }
];

$project.autocomplete({
  minLength: 0,
  source: projects,
  focus: function(event, ui) {
    this.val(ui.item.label);
    return false;
  }
});

$project.data("ui-autocomplete")._renderItem = function(ul, item) {

  var $li = $('<li>'),
    $img = $('<img>');


  $img.attr({
    src: 'https://jqueryui.com/resources/demos/autocomplete/images/' + item.icon,
    alt: item.label
  });

  $li.attr('data-value', item.label);
  $li.append('<a href="#">');
  $li.find('a').append($img).append(item.label);

  return $li.appendTo(ul);
};
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<link href="https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>

<div id="project-label">This one works:</div>
<input class="project">
<br />
<br />
<br />
<br />

<div id="project-label">This one does not show Images in the List:</div>
<input class="project">

为什么在第二个自动完成输入字段中没有显示图像。我怎样才能让它工作。 由于在我的网站中,我使用 php 自动生成字段并且总体上只有一个自动完成功能,因此我通过类调用自动完成功能。还有另一种可能吗?感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    只需用each 包装它,它就会调用集合中的每个项目。

    遍历一个 jQuery 对象,为每个匹配的元素执行一个函数。

    var $project = $('.project');
    
    
    var projects = [{
        value: "jquery",
        label: "jQuery",
        desc: "the write less, do more, JavaScript library",
        icon: "jquery_32x32.png"
      },
      {
        value: "jquery-ui",
        label: "jQuery UI",
        desc: "the official user interface library for jQuery",
        icon: "jqueryui_32x32.png"
      },
      {
        value: "sizzlejs",
        label: "Sizzle JS",
        desc: "a pure-JavaScript CSS selector engine",
        icon: "sizzlejs_32x32.png"
      }
    ];
    
    $project.autocomplete({
      minLength: 0,
      source: projects,
      focus: function(event, ui) {
        this.val(ui.item.label);
        return false;
      }
    });
    
    $project.each(function() {
      var $proj = $(this);
      
      $proj.data("ui-autocomplete")._renderItem = function(ul, item) {
        var $li = $('<li>'),
          $img = $('<img>');
    
    
        $img.attr({
          src: 'https://jqueryui.com/resources/demos/autocomplete/images/' + item.icon,
          alt: item.label
        });
    
        $li.attr('data-value', item.label);
        $li.append('<a href="#">');
        $li.find('a').append($img).append(item.label);
    
        return $li.appendTo(ul);
      };
    });
    <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
    <link href="https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
    <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
    
    <div id="project-label">This one works:</div>
    <input class="project">
    <br />
    <br />
    <br />
    <br />
    
    <div id="project-label">This one does not show Images in the List:</div>
    <input class="project">

    【讨论】:

    • 嘿。快速回答,我可以说:它有效!非常感谢您的帮助。
    猜你喜欢
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2016-03-30
    • 1970-01-01
    • 2021-08-26
    相关资源
    最近更新 更多