2

是否可以将一些 html 添加到 jquery 自动完成 ui 项?例如,我想将<strong></strong>标签添加到自动完成结果 item label: item['name']。我试过这样做label: '<strong>'+item['name']+'</strong>,但它是作为文本应用的,而不是 HTML。

$('input[name="customer"]').catcomplete({
    delay: 0,
    source: function(request, response) {
        $.ajax({
            url: 'index.php?route=sale/customer/autocompletefilter_name=' +  encodeURIComponent(request.term),
            dataType: 'json',
            success: function(json) {   
                response($.map(json, function(item) {
                    return {
                        category: item['customer_group'],
                        label: item['name'],
                        customer : item['name'],
                        value: item['customer_id'],
                        customer_group_id: item['customer_group_id'],
                        firstname: item['firstname'],
                        lastname: item['lastname'],
                        email: item['email'],
                        telephone: item['telephone'],
                        fax: item['fax'],
                        address: item['address']
                    }
                }));
            }
        });
    }, 
    select: function(event, ui) {
        //some actions on select
    }
});

谢谢你。

4

1 回答 1

5

查看 Scott Gonzales 的自动完成 HTML 扩展 - https://github.com/scottgonzalez/jquery-ui-extensions/blob/master/src/autocomplete/jquery.ui.autocomplete.html.js。它将允许您为标签选项传递 HTML。

示例- http://jsfiddle.net/eay3d/1/

$(function() {
    var availableTags = [
        { label: 'Apple1', value:'Apple1' },
        { label: '<strong>Apple2</strong>', value:'Apple2' },
        { label: 'Apple3', value:'Apple3' }             
    ];
    $( "#tags" ).autocomplete({
        source: availableTags,
        html: 'html'
    });
});

编辑 09/22/2013:源链接已修复,但不再维护

于 2012-07-08T10:29:49.577 回答