我正在学习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);
答案 0 :(得分:2)
// 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>