自动完成不适用于json

时间:2015-07-03 09:57:29

标签: javascript jquery json jquery-ui autocomplete

我需要什么:

  • 将数据附加到数组中,然后推送自动完成js。

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">

我挖掘的最终代码,但它也无效

    $(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});
   });      
  });

0 个答案:

没有答案