首页 > 解决方案 > Javascript表单未提交

问题描述

我正在制作一个转换温度的网页。我已经使用表格进行了所有设置,但我的答案不会提交。我尝试使用通过表单输入信息的方法,并尝试查看是否缺少任何内容但无法看到。

function fahrenheit() {
    //Declare variables
    var fahrenheit, celsius, c, f, crounded;
    //Receive values
    fahrenheit = document.getElementById("inputf").value;
    //Parse strings to numerics
    f = parseInt(fahrenheit);
    c = parseInt(celsius);
    //Calculations
    f = (9/5 * c) + 32;
    c = 5/9 * (f -32);
    crounded = c.toFixed(2);
    //Answer
    document.getElementById("first").value = crounded;
}

function celsius() {
    //Declares variables
    var fahrenheit, celsius, c, f, frounded;
    //Receive values
    celsius = document.getElementById("inputc").value;
    //Parse strings to numerics
    c = parseInt(celsius);
    f = parseInt(fahrenheit);
    //Calculations
    c = 5/9 * (f-32);
    f = (9/5 * c) + 32;
    frounded = f.toFixed(2);
    //Answer
    document.getElementById("second").value = frounded;
}
<h2>Temperature Conversion</h2>
        <div id="convertF" name="convertF">
            <form id="first" name="first">
                <p><label>Fahrenheit:</label><input type="number" id="inputf" name="inputf"></p>
                <p>Celsius:<input type="text" readonly id="outputc" name="outputc"></p>
                <input type="button" onclick="fahrenheit()" value="Submit Fahrenheit">
                <input type="reset" value="Reset">
            </form>
        </div>
        <br>
        <br>
        <div id="convertC" name="convertC">
            <form id="second" name="second">
                <p><label>Celsius:</label><input type="number" id="inputc" name="inputc"></p>
                <p>Fahrenheit:<input type="text" readonly id="outputf" name="outputf"></p>
                <input type="button" onclick="celsius()" value="Submit Celsius">
                <input type="reset" value="Reset">
            </form>

标签: javascriptforms

解决方案


需要将输出设置为输出文本框 id 而不是表单 id。

document.getElementById("first").value = crounded;

需要替换为

document.getElementById("outputc").value = crounded;

同样适用

document.getElementById("second").value = frounded;

应该

document.getElementById("outputf").value = frounded;

推荐阅读