在Javascript中将项目附加到API中的列表中

时间:2017-09-11 08:55:39

标签: javascript jquery list api

我试图通过设置我检索的所有商品(货币)的html属性来显示下拉列表中的货币。

这是我的代码:

var currencyDropdownData = "";
currencyDropdownData.length = 0;
if (confirmationData != null)
{
    currencyDropdownData = confirmationData.Currencies;
    console.log(currencyDropdownData)

    var BudgetSelectData = "";
    //BudgetSelectData += '<option selected value="-1">Please Select</option>'
    for (var currency = 0; currency < currencyDropdownData.length; currency++) {
        console.log(currencyDropdownData[currency].Name);
        BudgetSelectData += '<option value="' + currencyDropdownData[currency].Id + '">' + currencyDropdownData[currency].CurrencyText + currencyDropdownData[currency].Name  + '</option>'
    }

    console.log(BudgetSelectData);

    $('#budgetCurrency').html(BudgetSelectData);        
}

这是html:

<div class="row rowSpace">
    <div class="col-md-12">
        <div class="col-md-3">
            <label>Budget (Optional)</label>
        </div>
        <div class="col-md-6">
            <div class="col-md-10 col-sm-10 col-xs-11 noPadding">
                <select validateme="true" class="form-control fullWidth" id="budgetCurrency" placeholder="€ (Euro)" data-validation="length alphanumeric" data-validation-length="min1">
            </select>
        </div>
        <div class="col-md-2 col-sm-2 col-xs-1 noPadding" linkedval="budgetCurrency"></div>
    </div>
</div>

在控制台中,我可以看到html正在正确创建并且每种货币都会回来,但是下拉列表中没有显示任何内容

1 个答案:

答案 0 :(得分:0)

也许这就是你要找的东西

for (var currency = 0; currency < currencyDropdownData.length; currency++) {
    console.log(currencyDropdownData[currency].Name);
    var option = $("option").attr("value", currencyDropdownData[currency].Id)
                   .val(currencyDropdownData[currency].CurrencyText + currencyDropdownData[currency].Name);
    $('#budgetCurrency').append(option);
    }