【问题标题】:How to add an html element in JQuery UI Autocomplete?如何在 JQuery UI 自动完成中添加一个 html 元素?
【发布时间】:2020-08-16 09:57:15
【问题描述】:

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

这是我想用 Suggestions 作为标题来实现的目标:

到目前为止,这是我的脚本:

$(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】:

    看起来您需要通过覆盖自动完成项的呈现方式将其添加为“类别”(请参阅​​:https://jqueryui.com/autocomplete/#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 );
        });
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2011-10-03
      • 2014-01-21
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      相关资源
      最近更新 更多