JavaScript表单未提交

时间:2018-10-02 03:08:57

标签: javascript forms

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

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>

3 个答案:

答案 0 :(得分:0)

请在下面检查更新的代码:

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("outputc").value = crounded;*/
    x = (document.getElementById("inputf").value -32) * 5 / 9;
    document.getElementById("outputc").value = Math.round(x);
}

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("outputf").value = frounded;*/
    x = document.getElementById("inputc").value * 9 / 5 + 32;
    document.getElementById("outputf").value = Math.round(x);
}
<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>

答案 1 :(得分:0)

function fahrenheit() {
    //your codes here
    document.getElementById("first").submit();
}

function celsius() {
    //your codes here
    document.getElementById("second").submit();
}

答案 2 :(得分:0)

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

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

需要替换为

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

相同

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

应该是

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