简体   繁体   中英

How can I add custom HTML to a jQuery autocomplete item?

Is it possible to add some html to jquery autocomplete ui item? For example, I'd like to add <strong></strong> tags to autocomplete resulting item label: item['name'] . I've tried to do label: '<strong>'+item['name']+'</strong> , but it is applied as text, not 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
    }
});

Thnak you.

Check out Scott Gonzales' autocomplete HTML extension - https://github.com/scottgonzalez/jquery-ui-extensions/blob/master/src/autocomplete/jquery.ui.autocomplete.html.js . It'll allow you to pass in HTML for the label option.

Example - 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'
    });
});

Edit 09/22/2013 : Source link repaired, but no longer maintained .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM