如何在JQuery UI自动完成中添加html元素?

问题描述 投票:0回答:1

我最近使用了JQuery UI自动完成功能,想知道如何在列表顶部添加一个标题。

下面是我想实现的 建议 作为头。enter image description here

这是我目前的脚本

$(document).ready(function() {

$('.search').autocomplete({
source: 'search.php',
minLength: 1,
select: function(event,ui) {
var postid = ui.item.id;

if(postid != '') {
location.href = "post/" + postid;
}
}
});
});

我想添加 <span>Suggestions</span>.

javascript html jquery jquery-ui jquery-ui-autocomplete
1个回答
0
投票

看起来你需要通过覆盖自动完成项目的渲染方式来将其添加为一个 "类别"(见......)。https:/jqueryui.comautocomplete#categories)。). 代码未经测试,但可能会让你走上正确的道路。

$('.search').autocomplete({
  source: 'search.php',
  minLength: 1,
  select: function(event,ui) {
    var postid = ui.item.id;

    if (postid != '') {
      location.href = "post/" + postid;
    }
  },

  _create: function() {
    this._super();
    this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
  },
  _renderMenu: function( ul, items ) {
    var self = this;

    // Add your unselectable "category"
    ul.append( "<li class='ui-autocomplete-category'>SUGGESTIONS</li>" );

    // Add your other items
    $.each( items, function( index, item ) {
      self._renderItem( ul, item );
    });
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.