簡體   English   中英

"使用 JSON 和 JavaScript 創建隱藏貨幣的下拉菜單"

[英]Creating dropdowns to covert currency using JSON and JavaScript

任何人都可以技術我如何創建一個簡單的下拉選擇來使用java轉換貨幣。

\/\/在html中:

<select id="currency" onchange="getSelectValue();">
    <option value="Singapore Dollar">Singapore Dollar</option>
    <option value="USD">USD</option>
    <option value="Malaysia Riggit">Malaysia Riggit</option>
</select>

這是代碼示例。

var json=JSON.parse( "Your json which you mentioned above");

var products= json.products;



string selectlist="";

if(products.length>0)
{
    
    selectlist += "<select>";
    for(var i = 0; i < products.length; i++) {


        var currency = json[i];

            selectlist += "<option value="+currency.code+"> "+currency.code+"  </option>";
    }   
    selectlist += "</select>"
if("body").append(selectlist);
}

您可以像這樣在javascript中創建一個選擇,關注<script \/><\/code>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Javascript - Select</title>
</head>
<body>
    <div id="main"></div>
</body>
<script>
    const select = document.createElement("select");
    const {products} = JSON.parse('{ "products": [ { "name": "Apple", "price": "2.50", "code": "SGD" }, { "name": "Orange", "price": "2.50", "code": "SGD" } ] }');

    products.forEach(({code, name}) => {
        const option = document.createElement("option");
        option.label = name;
        option.value = code;
        select.appendChild(option);
    });

    const main = document.getElementById('main');
    main.appendChild(select);
</script>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM