我正在尝试使用引导程序的 typeahead() 函数来更新基于使用 JSON 的用户条目的输入。我得到了 JSON 对象,将其格式化为键:值对,现在我需要以某种方式将其推送到 typeahead 函数。我已经尝试对代码进行评论,以便您在阅读时了解我正在尝试做的事情。无论如何,到目前为止我有
jQuery...
$("#search").on("input", function() {
// Grab the data that we're going to use based on the user's input
$.getJSON( "http://holidays.allinclusive.co.uk/external/destinations.ashx?query=" + $("#search").val() )
// Once we've got it...
.done(function(data) {
// Create a new JSON object
var myObject = { 'destinations': [] };
// Loop through each of the 'suggestions' and for each one...
$.each(data.suggestions, function (index, elem) {
// Create a new object
var currentObject = {}
// Then we're going to use key value pairs {value: data.value, location: suggsestion.location}
currentObject['value'] = [data.data[index]];
currentObject['location'] = elem;
// Push each object to myObject
myObject.destinations.push(currentObject);
});
// Now with the myObject we're going to use it for .typeahead() on input#search
$('#search').typeahead({
// Grab the object we need
source: myObject,
// Now let's select the value from whatever the user wants
updater: function(item) {
selectedDestination = myObject.destinations[ data.data[index] ];
return item;
}
});
}); //.done
}); //.on
最后是 HTML...
<div class="well">
<input type="text" id="search" data-provide="typeahead" data-items="5">
</div>
有人可以帮我把这个拼凑起来吗?我花了 2 天时间在论坛和网络上寻找,现在已经没有头发可以拔了!
谢谢