javascript - Javascript表单未提交
问题描述
我正在制作一个转换温度的网页。我已经使用表格进行了所有设置,但我的答案不会提交。我尝试使用通过表单输入信息的方法,并尝试查看是否缺少任何内容但无法看到。
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>
解决方案
需要将输出设置为输出文本框 id 而不是表单 id。
document.getElementById("first").value = crounded;
需要替换为
document.getElementById("outputc").value = crounded;
同样适用
document.getElementById("second").value = frounded;
应该
document.getElementById("outputf").value = frounded;
推荐阅读
- python - 工作程序和驱动程序中的 Python 版本不同
- c++ - 使用 C++ 使用 GDK 获取像素 RGB 颜色空间
- ios - 关闭动作按钮项目上的动作按钮单击swift 4.2
- pandas - 如何插入空行并让数据从某个行号开始?
- java - 线程“main”java.lang.NumberFormatException 中的异常:对于输入字符串:“'\'All\''”
- android - 您的应用包含公开的 Google Cloud Platform (GCP) API 密钥。有关详细信息,请参阅此 Google 帮助中心文章
- tensorflow - InvalidArgumentError(参见上面的回溯):indices[47,6] = 24 is not in [0, 23)
- javascript - 使用`index.js`作为入口运行node.js服务器时,Module.compile出现语法错误“��c”
- docker - 卡在 ContainerCreating
- node.js - 使用 Axios/Sharp 下载和调整图像大小