Twitter Bootstrap Typeahead - Id & Label

前端 未结 10 2173
情书的邮戳
情书的邮戳 2020-12-12 13:06

I\'m using Bootstrap 2.1.1 and jQuery 1.8.1 and trying to use Typeahead\'s functionality.

I try to display a label and use an id li

10条回答
  •  忘掉有多难
    2020-12-12 13:53

    As of version 0.10.1 of Twitter Typeahead (https://github.com/twitter/typeahead.js), Id / Label is supported natively:

      $('input[name=address]').typeahead({
            hint: false
        }, {
            source: function (query, cb) {
                $.ajax({
                    url: '/api/addresses?q=' + encodeURIComponent(query),
                    dataType: 'json',
                    cache: false,
                    type: 'GET',
                    success: function (response, textStatus, jqXHR) {
                        cb(response.data);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                    }
                });
            },
            name: 'addresses',
            displayKey: 'text'
        }).on('typeahead:selected', function (e, suggestion, name) {
            window.location.href = '/' + suggestion.id;
        });
    

    If the example above, I'm passing an array of objects to the source callback (cb). By specifying displayKey: 'text', I'm telling the library to use the 'text' property for the auto-suggest. When the 'typeahead:select' callback is called, the second argument passed in (suggestion) contains the object that was selected.

提交回复
热议问题