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/

相关文章:

c# - 音频转换 C#

javascript - 使用javascript显示隐藏html div

javascript - Angular 4 Swiper集成(js初始化)

javascript - 如何从另一个页面链接到特定的 javascript 选项卡?

jquery - 多选的返回值

php - 将图像从逐行转换为隔行而不损失质量或细节?

javascript - Chart.js 更新函数(图表、标签、数据)不会更新图表

javascript - 在发布然后使用 BCRYPT 之前,我应该如何散列密码?

html - 如何消除 <embed> 元素周围的空白?

css - 专家PDF : How to match contents for converting from HTML page to PDF