如何使用javascript

时间:2018-03-29 00:49:13

标签: javascript html json

大家好,请大家帮忙,我需要在url中使用JSON数据包含JSON数据somexample.json到html选择droplist与javascript。 我试图找到解决方案,但没有得到解决方案,请任何人帮助我。

{
    "细节":{
    " numberOfBundles":14,       "束":[
         {
            "量":100.0,             "说明":" 200分钟Onnet语音",             " TYPECODE":153          },          {
            "量":1200.0,             " description":" 1GB Data Bundle",             " TYPECODE":101          },          {
            "量":400.0,             "描述":" 5GB Night&周末数据包",             " TYPECODE":162          },          {
            "量":3000.0,             " description":" 3GB Data Bundle",             " TYPECODE":102          },          {
            "量":5000.0,             " description":" 5GB Data Bundle",             " TYPECODE":161          },          {
            "量":5000.0,             "描述":" 7GB随时数据计划",             " TYPECODE":164          },          {
            "量":7500.0,             "描述":" 10GB夜晚&周末数据包",             " TYPECODE":144          },          {
            "量":9000.0,             " description":" 10GB Data Bundle",             " TYPECODE":103          },          {
            "量":14000.0,             "说明":" 20GB夜晚&周末数据包",             " TYPECODE" 145          },          {
            "量":17000.0,             " description":" 20GB Data Bundle",             " TYPECODE":104          },          {
            "量":36000.0,             " description":" 50GB Data Bundle",             " TYPECODE":105          },          {
            "量":70000.0,             " description":" 100GB Data Bundle",             " TYPECODE":142          },          {
            "量":5E + 5,             " description":" 500MB Data Bundle",             " TYPECODE":100     },     {
    "量":5E + 5,     " description":" 200GB Data Bundle",     " TYPECODE":143     }     ]     }     }

我需要html选择。

<select name="internetbundle">
<option value="500.00">5GB Night & Weekend Data Bundle</option>
<option value="3000.00">3GB Data Bundle</option>
<option value="5000.00">7GB Anytime data plan</option>
</select>

1 个答案:

答案 0 :(得分:-1)

你只需要遍历bundle并通过jQuery追加。

json.details.bundle.forEach(function(value) {   
var value = value.amount
var data = value.description
var id = value.typeCode
$('select[name="internetbundle"]').append('<option id="'+id+'" 
    value="'+value+'">'+data+'</option>');
)}