为简单起见,让我们从这里的第一个最简单的例子开始:https://twitter.github.io/typeahead.js/examples/
以下是此示例的建议数组:
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut' etc..
];
我想在这个数组中有对象:
var states = [
{name: 'Alabama', image: 'images/Alabama.jpg', objectId: '123'},
{name: 'Alaska', image: 'images/Alaska.jpg', objectId: '341'},
{name: 'Arizona', image: 'images/Arizona.jpg' objectId: '546'},
];
当我按“输入”键或点击其中一个建议时,我需要提取此特定对象并将.push
提取到另一个数组。
我该如何实现呢?
答案 0 :(得分:1)
$(textbox-id).on('typeahead:selected', function (e, datum) {
console.log(datum); //object
console.log(datum.value); // will print Value you have clicked like Alabama is selected then Alabama is printed in the console.