我需要什么:
JSON:
{
"industry": [
{
"id": 16,
"name": "Agriculture & Forestry",
"industry_url": "agriculture-forestry"
},
{
"id": 3,
"name": "Apparel & Clothing",
"industry_url": "apparel-fashion"
},
{
"id": 56,
"name": "Architecture & Designing",
"industry_url": "architecture"
},
{
"id": 83,
"name": "Astrology",
"industry_url": "astrology"
}
]
}
码
if (http.readyState == 4 && http.status == 200)
{
if (obj = JSON.parse(http.responseText), obj.industry.length > 0)
{
industry = document.getElementById("industry_url").getAttribute("value");
var industry = [];
for (var b = 0; b < obj.industry.length; b++)
{
//console.log(obj.industry[b]);
var indus=obj.industry[b];
var temp = new Object();
temp["name"] = indus.name;
temp["event_url"] = indus.industry_url;
industry.push(temp);
console.log(industry);
$('.autocomplete').autocomplete({
lookup: industry,
onSelect: function(suggestion) {
var thehtml = '<strong>industry name:</strong> ' + suggestion.name + ' <br> <strong>'+ suggestion.industry_url+'</strong>';
$('#outputcontent').html(thehtml);
}
});
HTML:
<input type="text" autocomplete="off" name="Industry" id="industry_name"
class="biginput autocomplete" placeholder="All Industry" id="ex3">
我试图谷歌找到jquery library。
逻辑我发现,数据首先会推送到数组中自动完成数组。
我挖掘的最终代码,但它也无效
$(document).ready(function(){
var arrayAutocomplete = new Array();
$.getJSON('apiurl', function(json)
{
console.log(json);
$.each(json.country,function(index, value){
console.log(value);
arrayAutocomplete = new Array();
arrayAutocomplete['text'] = value.text;
arrayAutocomplete['country_url'] = value.country_url;
});
$("#country_name").autocomplete({source: arrayAutocomplete});
});
});