简体   繁体   English

"使用 JSON 和 JavaScript 创建隐藏货币的下拉菜单"

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

can anyone tech me how can I create a simple dropdown selection to convert the currency using java.任何人都可以技术我如何创建一个简单的下拉选择来使用java转换货币。

\/\/in html : \/\/在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>

here is the code sample.这是代码示例。

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

You can create a select in javascript like this, focus on the <script \/><\/code>您可以像这样在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