繁体   English   中英

简单的货币转换脚本不起作用

[英]Simple currency conversion script not working

我正在开发一个简单的javascript货币转换应用程序。 问题是,无论如何我都不会修补它。 输入字段不接受任何输入,并且在尝试输入任何数字时仅显示“ 0”。

我最初认为PHP代码可能是问题,但显然它应该没有问题。

<h1 style='font-size:46px'>1 DOLLAR = <input readonly id="currentprice" type="number">
<?php $url = "https://api.fixer.io/latest?base=USD"; 
    $json = file_get_contents($url); 
    $json_data = json_decode($json, true); 
    $price = $json_data["rates"]["BRL"]; 

    echo $price; 
 ?> 
    />
</h1>

How many Dollars?
<input oninput='finalAmountUSD()' onchange='finalAmountUSD()' style='font-size:23px;height:45px' type='number' class="buy buyinput form-control" id='usdamount' required value='0.00000000' tabindex="1" />
How many BRL?
<input oninput='finalAmountBRL()' onchange='finalAmountBRL()' style='font-size:23px;height:45px' type='number' class="buy buyinput form-control" id='brlamount' required value='0.00' tabindex="2" />

<script>
    function finalAmountUSD() {
        x = document.getElementById('currentprice').value;
        y = document.getElementById('usdamount').value;
        z = document.getElementById('brlamount').value;

        document.getElementById('usdamount').value = x * z;
    }
    function finalAmountBRL() {
        x = document.getElementById('currentprice').value;
        y = document.getElementById('usdamount').value;
        z = document.getElementById('brlamount').value;

        document.getElementById('brlamount').value = x * y;
    }
</script>

您正在使用两个事件处理程序oninput和onchange。 在元素的值更改后立即触发oninput事件处理程序,而在元素的值更改并且元素失去焦点时触发onchange事件处理程序。

我认为您只需要oninput事件处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM