我有以下 JSON 对象,我想使用这些值来填充下拉列表。我一直试图让它工作 2 天,并准备把我的电脑扔出窗外。有人可以帮帮我吗?!
这是 JSON 对象...
{
"query":"de mar",
"suggestions":[
"Any Location",
"Camp De Mar, Majorca, SPAIN",
"L'amettla De Mar, Costa Dorada, SPAIN",
"Lloret De Mar, Costa Brava, SPAIN",
"Malgrat De Mar, Costa Brava, SPAIN",
"Pineda De Mar, Costa Brava, SPAIN",
"Roquetas De Mar, Costa De Almeria, SPAIN",
"Tossa De Mar, Costa Brava, SPAIN"
],
"data":[
"",
"DestinationResort|Camp De Mar",
"DestinationResort|L'Amettla De Mar",
"DestinationResort|Lloret De Mar",
"DestinationResort|Malgrat De Mar",
"DestinationResort|Pineda De Mar",
"DestinationResort|Roquetas De Mar",
"DestinationResort|Tossa De Mar"
]
}
还有我的 html 和 javascript...
<input type="text" id="search" data-provide="typeahead">
<select name="destinations" id="destinations"></select>
$("#search").on("input", function() {
$.getJSON( "http://holidays.allinclusive.co.uk/external/destinations.ashx?query=" + $("#search").val() )
.done(function(data) {
$.each(data, function() {
$.each(this, function(name, value) {
$( '<option value="' + data.value +'">' + value + '</option>' ).appendTo( "#destinations" );
})
})
})
});
最终我正在寻找这样的东西......
<option value="DestinationResort|Camp De Mar">Camp De Mar, Majorca, SPAIN</option>
有人可以结束我的痛苦吗?
谢谢