【问题标题】:Basic javascript Fahrenheit/Celsius calculator基本的javascript华氏/摄氏度计算器
【发布时间】:2016-05-21 07:39:47
【问题描述】:

我正在尝试做一个摄氏/华氏转换计算器,每次转换都有一个按钮,结果显示在相应的文本框中。我必须在这里遗漏一些明显的东西......我是javascript的新手,只是没有得到它。这是我到目前为止所做的:

<!DOCTYPE html>
<html>
    <head> 
        <meta charset = "utf-8"/>
        <title>Convert Fahrenheit and Celsius</title>
        <script type="text/javascript">
            <!--
                function convertToC() {
                    var fTempVal = parseFloat(document.getElementById('fTemp').value);
                var cTempVal = (fTempVal - 32) * (5/9);
                document.getElementById('cTemp').value = cTempVal;
            }

            function convertToF() {
                var cTempVal = parseFloat(document.getElementById('cTemp').value);
                var fTempVal = (cTempVal * (9/5)) + 32;
                document.getElementById('fTemp').value = fTempVal;
            }
        // -->
    </script>
    </head>
    <body>
    <form name="conversionForm">
    <table border="1">
    <tbody>
    <tr>
        <td>Fahrenheit</td>
        <td><input name="fTemp" type="text"/></td>
        <td><button onclick="convertToC">Convert to Celsius</button></td>
    </tr>
    <tr>
        <td>Celsius</td>
        <td><input name="cTemp" type="text"/></td>
        <td><button onclick="convertToF">Convert to Fahrenheit</button></td>
    </tr>
    </form>
    </tbody>
    </table>
</body>
</html>

【问题讨论】:

  • 那有什么问题?
  • 要么它没有进行计算,要么它只是没有在文本框中显示它......无法弄清楚哪个。或者两者兼而有之?

标签: javascript html forms


【解决方案1】:

您有一些错误。请参阅此已更正的 jsFiddle example

  1. 您的输入框缺少您尝试使用 document.getElementById 引用的 ID
  2. 您的 HTML 未正确关闭。
  3. 您的按钮缺少类型,这使得它们成为 default to submit 当您真正想要按钮时
  4. 为防止表单被实际提交,您应该返回 false。

JavaScript

    function convertToC() {
        var fTempVal = parseFloat(document.getElementById('fTemp').value);
        var cTempVal = (fTempVal - 32) * (5 / 9);
        document.getElementById('cTemp').value = cTempVal;
        return false;
    }

    function convertToF() {
        var cTempVal = parseFloat(document.getElementById('cTemp').value);
        var fTempVal = (cTempVal * (9 / 5)) + 32;
        console.log(fTempVal);
        document.getElementById('fTemp').value = fTempVal;
        return false;
    }

HTML

<form name="conversionForm">
    <table border="1">
        <tbody>
            <tr>
                <td>Fahrenheit</td>
                <td>
                    <input name="fTemp" id="fTemp" type="text" />
                </td>
                <td>
                    <button type="button" onclick="convertToC();return false">Convert to Celsius</button>
                </td>
            </tr>
            <tr>
                <td>Celsius</td>
                <td>
                    <input name="cTemp" id="cTemp" type="text" />
                </td>
                <td>
                    <button type="button" onclick="convertToF();return false">Convert to Fahrenheit</button>
                </td>
            </tr>
        </tbody>
    </table>
</form>

【讨论】:

    【解决方案2】:

    您正在使用 document.getElementById() 函数查询 DOM,但两个 input 元素当前都没有 id 属性,只有 name 属性。因此,在您的示例中,没有任何元素与您传递给函数的 ID 实际匹配。

    您应该像这样设置两个元素的id 属性:

    <input id="fTemp" name="fTemp" type="text">
    
    <input id="cTemp" name="cTemp" type="text">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多