gpt4 book ai didi

javascript - 将摄氏度转换为华氏度 - 如何在输入中显示

转载 作者:可可西里 更新时间:2023-11-01 13:22:32 28 4
gpt4 key购买 nike

我正在学习 Javascript,我正在尝试创建一个简单的脚本,该脚本采用用户摄氏度并将其转换为华氏度输入字段内的华氏度。我能够控制台记录结果,但我不知道如何在输入字段中输入华氏温度。 Codepen Here

//Capture input string from .inputbox into TestVar
function testResults (form) {
var TestVar = form.inputbox.value,
numString = parseFloat(TestVar),
text = "";

return numString * 1.8 + 32;

}
// Celsius * 1.8 + 32 = Fahrenheit
console.log(testResults);

最佳答案

您可以使用 HTMLInputElement.value 来做到这一点

 // Find the button in the document
let btn = document.querySelector('input[type=button]');
// Add a click event listener to the button
btn.addEventListener ('click', e => {
// Find the fahrenheit input field
let f = document.querySelector('#fahrenheit');
// Find the celsisus input field
let c = document.querySelector('#celsisus');
// Make the calculation from the fahrenheit value.
// Save it to the celsisus input field using `c.value`
// where `c` is the reference to the celsisus input
c.value = parseFloat(f.value) * 1.8 + 32;
});
<form name="myform" action="" method="GET">
<p>Convert Celsius to Fahrenheit </p>
<p><input type="text" name="inputbox" value="" id="fahrenheit" placeholder="Fahrenheit"></p>
<p><input type="text" name="fahrenheit" id="celsisus" value="" placeholder="Celsius"></p>
<p><input type="button" NAME="button" Value="Click" ></p>
</form>

关于javascript - 将摄氏度转换为华氏度 - 如何在输入中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45153765/

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