gpt4 book ai didi

javascript - 不会转换温度

转载 作者:行者123 更新时间:2023-11-30 08:08:00 25 4
gpt4 key购买 nike

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

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;
}

最佳答案

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

document.tempForm.resultField.value = result;

关于javascript - 不会转换温度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14786996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com