【问题标题】:Display an icon in jQuery UI autocomplete results在 jQuery UI 自动完成结果中显示一个图标
【发布时间】:2010-09-22 23:19:09
【问题描述】:

我正在使用jQuery UI Autocomplete plugin(1.8 版),我想自定义显示建议的方式。具体来说,我不仅想显示一些文本,还想显示一个图标。但是,当我发送 标记时,它只会在结果列表中呈现为纯文本。

有什么方法可以改变这种行为吗?或者,您能否建议一种不同的方式来将图像包含在返回的结果中并让它们显示在建议中?

【问题讨论】:

    标签: jquery-ui-autocomplete


    【解决方案1】:

    取自here

    $("#search_input").autocomplete({source: "/search",
                                     minLength: 3,
                                     select: function (event, ui) {
                                         document.location = ui.item.url;
                                     }
    })
    .data("autocomplete")._renderItem = function (ul, item) {
    //As per recent documemtation above line should be 
    //.autocomplete( "instance" )._renderItem = function (ul, item) {
        return $('<li class="ui-menu-item-with-icon"></li>')
            .data("item.autocomplete", item)
            .append('<a><span class="' + item.type + '-item-icon"></span>' + item.label + '</a>')
            .appendTo(ul);
    };
    

    还有 CSS:

    .ui-menu .ui-menu-item-with-icon a {
      padding-left: 20px;
      
    }
    span.group-item-icon,
    span.file-item-icon {
      display: inline-block;
      height: 16px;
      width: 16px;
      margin-left: -16px;
    }
    span.group-item-icon {
      background: url("/image/icons/group.png") no-repeat left 4px;
    }
    span.product-item-icon {
      background: url("/image/icons/product.png") no-repeat left 7px;
    }
    

    【讨论】:

    猜你喜欢
    • 2015-08-09
    • 2011-10-12
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多