JavaScript - 从数组中提取数据(货币转换

时间:2017-10-11 19:58:09

标签: javascript arrays json api

晚上所有,(第一篇文章所以请温柔,因为我对这一切都很陌生(并编码本身))。

我正在为一个小项目创建货币转换页面。但是我无法从数组中获取数据,然后乘以用户输入。作为默认值,我可以将所有货币的值设为1.即GBP = 1到USD = 1.3187。

我只是无法获得用户输入的详细信息(问题非常简单,但我正在敲打我的头脑)。

数组信息如此显示。巴西雷亚尔:2.4704,加元:0.97445

到目前为止我的代码:

//FUNCTION
function currencyConvert() {
    var myCurrency = document.getElementById("currency").value; //selected user currency
    var conversion = document.getElementById("amount").value; //amount to be converted

    //AJAX API TO CHECK IF THERE IS INTERNET
    var myReq = new XMLHttpRequest();
    //API ADDRESS
    var currData = "http://api.fixer.io/latest?" + "base=" + myCurrency;

    console.log(currData);

    myReq.open("GET", currData, true);
    //GET REQUEST TO SERVER
    myReq.send();

    myReq.onload = function() {
            //AJAX SERVER
            if (myReq.readyState == 4 && myReq.status == 200) {

                var result = myReq.responseText;
                /*console.log(myReq);*/
                console.log(result);

                //CONVERT JSON TO JAVASCRIPT
                var jsResult = JSON.parse(result);
                console.log(jsResult);

                /*var convertedRate = conversion * jsResult.rates
                console.log(convertedRate);*/

                //PULL BASE CURRENCY TO CHANGE FROM DROPDOWN

                /*var unitOne = jsResult.base;
                console.log(unitOne);*/

                /* jsResult.base = myCurrency;
                console.log(myCurrency);
                */
                //AMOUNT FROM USER INPUT BOX
                /*var amount = document.getElementById("amount").value;*/

                //CALCULATE THE INPUT BOX FOR BASE AGAINST CURRENCY CHOSEN
                /*console.log(myCurrency.rates);*/
                /*document.getElementById("converted").value = (unitOne*amount).toFixed(2);*/

                //DISPLAY CURRENCIES AGAINST BASE CURRENCY (CURRENT DEFAULT VALUE FROM AMOUNT =1)

                /*var conTotal = jsResult.rates*conversion;*/
                var conTotal = jsResult.rates;
                console.log(conTotal);



                document.getElementById('aud').innerHTML = jsResult.rates.AUD
                document.getElementById('cad').innerHTML = jsResult.rates.CAD
                document.getElementById('chf').innerHTML = jsResult.rates.CHF
                document.getElementById('cny').innerHTML = jsResult.rates.CNY
                document.getElementById('dkk').innerHTML = jsResult.rates.DKK
                document.getElementById('eur').innerHTML = jsResult.rates.EUR
                document.getElementById('gbp').innerHTML = jsResult.rates.GBP
                document.getElementById('hkd').innerHTML = jsResult.rates.HKD
                document.getElementById('huf').innerHTML = jsResult.rates.HUF
                document.getElementById('inr').innerHTML = jsResult.rates.INR
                document.getElementById('jpy').innerHTML = jsResult.rates.JPY
                document.getElementById('mxn').innerHTML = jsResult.rates.MXN
                document.getElementById('myr').innerHTML = jsResult.rates.MYR
                document.getElementById('nok').innerHTML = jsResult.rates.NOK
                document.getElementById('nzd').innerHTML = jsResult.rates.NZD
                document.getElementById('rub').innerHTML = jsResult.rates.RUB
                document.getElementById('sek').innerHTML = jsResult.rates.SEK
                document.getElementById('sgd').innerHTML = jsResult.rates.SGD
                document.getElementById('thb').innerHTML = jsResult.rates.THB
                document.getElementById('try').innerHTML = jsResult.rates.TRY
                document.getElementById('usd').innerHTML = jsResult.rates.USD
                document.getElementById('zar').innerHTML = jsResult.rates.ZAR

            } //end of if
        } //end of secondary function
} //end of main function*/

0 个答案:

没有答案