1

我正在研究一个从摄氏度到华氏度以及从华氏度到摄氏度的简单转换表格。我不知道为什么它没有转换。

HTML

<head>
    <script type="text/javascript" src="script.js"></script>
</head>

<body>
    <form name="tempForm">
        <label for="temp">Temperature:</label>
        <input type="text" id="temp"><br>

        <input type="radio" name="choice" value="fahrenheit" checked />Convert to Fahrenheit <br>
        <input type="radio" name="choice" value="celsius">Convert to Celsius  <br>

        <label for="resultField">Result: </label>
        <input type="text" id="resultField"><br>

        <input type="button" value="Convert" onclick="processForm()">
    </form>

</body>

Javascript 函数 processForm() {

var temperature = Number(document.tempForm.temp.value);
var tempType;
var result;

for (var i=0; i < document.tempForm.choice.length; i++) {

    if (document.tempForm.choice[i].checked) {
        tempType = document.tempForm.choice[i].value;
    }
}

if (tempType == 'fahrenheit') {
    result = temperature * 9/5 + 32;
}

else {
    result = (temperature -  32)  *  5/9;
}

// Assign the result field value here
result = document.tempForm.resultField.value;
}
4

2 回答 2

5

您在最后分配了错误的结果。您必须将分配的目标放在 assingment 的左侧,因此您的结果字段和右侧是您要分配给它的值,如下所示:

document.tempForm.resultField.value = result;
于 2013-02-09T10:11:43.040 回答
1

您的转换正在工作,但您resultField以错误的方式分配结果。

像这样转换作业(最后一个)

document.tempForm.resultField.value  = result;
于 2013-02-09T10:16:55.763 回答