javascript - 货币转换器不适用于 Javascript 和 HTML

标签 javascript html converters

我正在为加密货币制作一个货币转换器,但我在使用它时遇到了问题。这是源代码。现在我尝试了几种方法让它起作用,但我要么没有成功,要么只部分成功。

HTML:

         <html>
           <head>
           </head>
           <body>

            <input oninput="convert()" onchange="convert()"  type="value" name="input" id="currencycoininsert" placeholder="0.00"/>
        <!--    <button type="button" id="btncur-insert"> -->
                <select name="dropdowncurrency" id="selectcurinsert">
                    <option value="BTC" id="selectcurinsert1" >BITCOIN (BTC)</option>
                    <option value="ETH" id="selectcurinsert2"  selected>ETHERIUM (ETH)</option>
                    <option value="LTC" id="selectcurinsert3" >LITECOIN (LTC)</option>
                    <option value="ZEC" id="selectcurinsert4" >ZCASH (ZEC)</option>
                    <option value="XMR" id="selectcurinsert5" >MONERO (XMR)</option>
                </select>
        <!--    </button> -->
            <button type="submit" name="smbtcur" id="sbmtcurinsert" onclick="convert()">
            Convert
            </button>
            <input type="value" id="received"/>

         </body>
         </html>

感谢任何帮助。 这是 Javascript 部分: JS:

                var rates = [10500, 920, 220, 310, 460];

                var BTCUSD = rates[0];
                var ETHUSD = rates[1];
                var LTCUSD = rates[2];
                var XMRUSD = rates[3];
                var ZECUSD = rates[4];

            function convert(){


                var x1 = document.getElementById("selectcurinsert1").value;
                var x2 = document.getElementById("selectcurinsert2").value;
                var x3 = document.getElementById("selectcurinsert3").value;
                var x4 = document.getElementById("selectcurinsert4").value;
                var x5 = document.getElementById("selectcurinsert5").value;
                if (document.getElementById("selectcurinsert").value = x1)
                {
                  function convertbtc()
                {   
                        z = BTCUSD;
                        x = document.getElementById("currencycoininsert").value;
                        document.getElementById("receivedmufg").value = x * z;   
                }
                }
                else if (document.getElementById("selectcurinsert").value = x2)
                {
                function converteth()
                {

                        z = BTCUSD;
                        x = document.getElementById("currencycoininsert").value;
                        document.getElementById("receivedmufg").value = x * z; 

                }
                }
                else if (document.getElementById("selectcurinsert").value = x3)
                {
                function converteth()
                {

                        z = BTCUSD;
                        x = document.getElementById("currencycoininsert").value;
                        document.getElementById("receivedmufg").value = x * z; 

                }
                }
                else if (document.getElementById("selectcurinsert").value = x4)
                {
                function converteth()
                {

                        z = BTCUSD;
                        x = document.getElementById("currencycoininsert").value;
                        document.getElementById("receivedmufg").value = x * z; 

                }
                }
                else if (document.getElementById("selectcurinsert").value = x5)
                {
                function converteth()
                {

                        z = BTCUSD;
                        x = document.getElementById("currencycoininsert").value;
                        document.getElementById("receivedmufg").value = x * z; 

                }
                }
                }


            </script>

感谢您的帮助,希望一切顺利。

最佳答案

convert 函数的定义中,您在条件语句中定义了其他函数,但您从未真正调用这些函数。

您可以在其他地方定义 converteth 等,然后在条件内部像 converteth() 一样调用它。

伪代码:

function converteth () {
    ... converteth code ...
}

function convertbtc () {
    ... convertbtc code ...
}

function convert () {
    ... get element by id statements
    if (document.getElementById("selectcurinsert").value = x1) {
        convertbtc();
    } ... other conditional statements ...
}

此外,在条件语句中,您使用赋值运算符 = 而不是逻辑运算符来比较两个值。我假设您想使用 =====

关于javascript - 货币转换器不适用于 Javascript 和 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48851835/

相关文章:

javascript - 当我路由到该组件时,如何将 vuejs 数组发送到另一个组件而不丢失数组数据?

html - 增加文本大小以仅适合一列的表格单元格

html - 有没有办法将图像中的 GUI 转换为 html?

javascript - 如何使用 Bootstrap Modals 和按钮制作可重用的确认控件?

javascript - 条件数据显示在 Angularjs 中隐藏

javascript - 使用 AngularJS 深度合并对象

HTML+CSS 用 <a> 和 <h> 格式化按钮

javascript - 什么会导致浏览器不执行脚本?

python - 如何从 NetworkX 图转换为 ete3 Tree 对象?

algorithm - 为什么 double-dabble 算法有效?