javascript中的货币转换器

时间:2017-01-11 21:27:51

标签: javascript jquery

我正在尝试制作货币转换器。我有2个输入和2种货币。这是我的HTML代码:

<table>
        <tr>
            <td> want to buy <input id="firstinput" onkeyup="convert('C')"></td>
            <td>
                <select id="from">
                    <option value="GEL" selected>EURO(EUR)</option>
                    <option value="USD">USdollar(USD)</option>
                </select>
            </td>
        </tr>
        <tr>
            <td> want to buy <input id="secondinput" onkeyup="convert('F')"></td>
            <td>
                <select id="to">
                    <option value="EUR">EUR0(EUR)</option>
                    <option value="USD" selected>USdollar(USD)</option>
                </select>
            </td>
        </tr>
</table>

当第一个选择是欧元而第二个选择是美元时,它应该乘以某个值(比如2)。当第一个选择是美元和第二个欧元时,则除以2.否则乘以1.

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

这是你的HTML: 我为了一致性做了一些改变。请仔细检查。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <title>repl.it</title>
        <script src="index.js"></script>
        <link href="index.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <table>
        <tr>
            <td> want to buy <input id="firstinput" onkeyup="convert('C');"></td>
            <td>
                <select id="from" onchange="convert('C')">
                    <option value="EUR" selected>EURO(EUR)</option>
                    <option value="USD">USdollar(USD)</option>
                </select>
            </td>
        </tr>
        <tr>
            <td> want to buy <input id="secondinput" onkeyup="convert('F')"></td>
            <td>
                <select id="to"  onchange="convert('F')">
                    <option value="EUR">EURO(EUR)</option>
                    <option value="USD" selected>USdollar(USD)</option>
                </select>
            </td>
        </tr>

        </table>

    </body>
</html>

这是您的javascript代码:

function convert(param1){
    var factor;
    f = document.getElementById('from').value
    t = document.getElementById('to').value

    if(f == t){
        factor = 1
    }
    else if(f == 'EUR' && t == 'USD'){
        factor = 1.06;
    }
    else if(f == 'USD' && t == 'EUR'){
        factor = 1/1.06;
    }

    if(param1 == "C"){
        document.getElementById('secondinput').value = document.getElementById('firstinput').value * factor
    }
    if(param1 == "F"){
        document.getElementById('firstinput').value = document.getElementById('secondinput').value * factor
    }

}

答案 1 :(得分:0)

使用JQuery Ajax .....

$('#result').val('Loading...');

var amount = $('#amount').val();
var from = $('#from').val();
var to = $('#to').val();



 $.ajax({
     type: "GET",
       url: "http://free.currencyconverterapi.com/api/v5/convert?q=" + from + "_" + to +"&compact=y",
    success: function(data) {
        var exchangeRate = JSON.stringify(data).replace(/[^0-9\.]/g,'');

        var result = amount*exchangeRate;
           $('#result').val(parseFloat(result).toFixed(2));
        }
    });