Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 用json填充select下拉列表_Jquery_Json - Fatal编程技术网

Jquery 用json填充select下拉列表

Jquery 用json填充select下拉列表,jquery,json,Jquery,Json,使用jquery,我试图用json填充select下拉列表。我正在尝试显示下面json中的国家列表 { "country":[ { "China":[ { "tarrifType":"Pay as you go" }, { "fixLine":"23p" } ], "India":[ {

使用jquery,我试图用json填充select下拉列表。我正在尝试显示下面json中的国家列表

{  
"country":[  
  {  
     "China":[  
        {  
           "tarrifType":"Pay as you go"
        },
        {  
           "fixLine":"23p"
        }
     ],
     "India":[  
        {  
           "sms":"39p"
        },
        {  
           "fixLine":"3p",
           "sms":"59p"
        }
     ],
     "Poland":[  
        {  
           "mobile":"29p",
           "sms":"19p"
        },
        {  
           "tarrifType":"China Pass",
           "fixLine":"23p"
        }
     ]
  }
]
}
到目前为止,我尝试使用的jquery如下所示

 $.getJSON("js/widgets/country-picker.json", function(result){
   $.each(result, function(i, field) {
  $('#js-rate-select').append($('<option>').text(field[1]).attr('value',    field[1]));
  });
 });

但它不会填充select下拉列表,也不会给出任何DOM错误。有什么建议可以解决这个问题吗?我认为问题在于json格式——我可以更改它,这要感谢您的json提要返回一个只包含一个索引的数组,这是一个不同国家/地区的对象。因此,你应该说

result.country[0].China.tarrifType.
并且,您可以重复以下国家:

$.each(result.country[0], function(country,data) { console.log(country); }
var result = {
    "country": {
        "China": {
            "tarrifType": "Pay as you go",
            "fixLine": "23p"
        },
        "India": {
            "sms": "39p",
            "fixLine": "3p",
            "sms": "59p"
        },
        "Poland": {
            "mobile": "29p",
            "sms": "19p",
            "tarrifType": "China Pass",
            "fixLine": "23p"
        }
    }
};


$(function () {
    $.each(result.country, function (country, data) {
        console.log("Country : " + country);
        console.log(data); // data.fixLine, data.tarrifType, data.sms

        $('#selectbox').append('<option value="' + country + '">' + country + '</option>');

    });
});
尽管如此,我还是建议您重做JSON。你可以这样做:

$.each(result.country[0], function(country,data) { console.log(country); }
var result = {
    "country": {
        "China": {
            "tarrifType": "Pay as you go",
            "fixLine": "23p"
        },
        "India": {
            "sms": "39p",
            "fixLine": "3p",
            "sms": "59p"
        },
        "Poland": {
            "mobile": "29p",
            "sms": "19p",
            "tarrifType": "China Pass",
            "fixLine": "23p"
        }
    }
};


$(function () {
    $.each(result.country, function (country, data) {
        console.log("Country : " + country);
        console.log(data); // data.fixLine, data.tarrifType, data.sms

        $('#selectbox').append('<option value="' + country + '">' + country + '</option>');

    });
});
请点击此处:

您实际希望在select中使用json的哪一部分?国家名称还是每个国家的一部分?非常感谢您的帮助,感谢您花时间拨弄。我还按照建议修改了json结构,它工作得很好。非常感谢。