Twitter类型自动完成动态添加输入

use*_*391 6 jquery typeahead.js twitter-typeahead

我在我的网站上使用Twitter typeahead,它工作正常.但是当我尝试动态添加新输入时,它不起作用.可能是什么问题呢?

谢谢你的回复.

    var custom = new Bloodhound({
    datumTokenizer: function(d) { return d.tokens; },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: 'http://'+window.location.hostname+'/invoice/loadItemOption?query=%QUERY'
    });

    custom.initialize();

    $('.typeahead_option_items').typeahead(null, {
          name: 'item_title[]',
          displayKey: 'invoice_item_option_title',
          source: custom.ttAdapter(),
          hint: (App.isRTL() ? false : true),
    }).on('typeahead:selected', function (obj, value) {
        console.log(value.invoice_item_option_title);
    });
Run Code Online (Sandbox Code Playgroud)

use*_*961 17

我做的是把它包装成一个函数

function typeahead_initialize() {
 var custom = new Bloodhound({
    datumTokenizer: function(d) { return d.tokens; },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: 'http://'+window.location.hostname+'/invoice/loadItemOption?query=%QUERY'
    });

    custom.initialize();

    $('.typeahead_option_items').typeahead(null, {
          name: 'item_title[]',
          displayKey: 'invoice_item_option_title',
          source: custom.ttAdapter(),
          hint: (App.isRTL() ? false : true),
    }).on('typeahead:selected', function (obj, value) {
        console.log(value.invoice_item_option_title);
    });
}

typeahead_initialize();
Run Code Online (Sandbox Code Playgroud)

而且在添加动态输入之前我运行

$('.typeahead_option_items').typeahead('destroy');
Run Code Online (Sandbox Code Playgroud)

并在创建元素之后

typeahead_initialize();
Run Code Online (Sandbox Code Playgroud)

适合我,希望这会有所帮助.