typeahead.js获取选定的数据

Sov*_*vos 16 typeahead typeahead.js

我正在尝试使用Twitter typeahead.js在选择后返回一个数据.从我对文档的理解,代码看起来应该是这样的

$('.selector').typeahead({
  name: 'identifier',
  local: localObjectsArray
}).on('autocompleted', function(item){
    alert(JSON.stringify(item));
});
Run Code Online (Sandbox Code Playgroud)

但这不起作用.检测先行事件的正确方法是什么?

Hie*_*yen 45

选择后,您需要typeahead:selected:

$('.selector').typeahead({
    name: 'identifier',
    local: localObjectsArray
}).on('typeahead:selected', function (obj, datum) {
    console.log(obj);
    console.log(datum);
});
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • 很公平,但仅仅是为了记录,"从jQuery 1.7开始,.on()方法是附加事件处理程序的首选方法"来自http://api.jquery.com/bind/ (4认同)
  • 请注意,自定义typeahead事件现在是`typeahead:select`而不是`typeahead:selected`.我不确定何时进行了此更改,但从v0.11.1开始,`typeahead.selected`未定义.文档反映了这一点:https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#custom-events (4认同)
  • 这是一个很好的提示,但应该编辑使用.on()而不是.bind()请参阅http://stackoverflow.com/questions/8065305/whats-the-difference-between-on-and-live-或绑定 (3认同)