Jes*_*hez 4 html javascript forms
我正在尝试使用Celsius/Fahrenheit转换计算器,每个转换都有一个按钮,结果显示在相应的文本框中.我必须在这里遗漏一些明显的东西...我是javascript的新手,我只是没有得到它.这是我到目前为止所做的:
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8"/>
<title>Convert Fahrenheit and Celsius</title>
<script type="text/javascript">
<!--
function convertToC() {
var fTempVal = parseFloat(document.getElementById('fTemp').value);
var cTempVal = (fTempVal - 32) * (5/9);
document.getElementById('cTemp').value = cTempVal;
}
function convertToF() {
var cTempVal = parseFloat(document.getElementById('cTemp').value);
var fTempVal = (cTempVal * (9/5)) + 32;
document.getElementById('fTemp').value = fTempVal;
}
// -->
</script>
</head>
<body>
<form name="conversionForm">
<table border="1">
<tbody>
<tr>
<td>Fahrenheit</td>
<td><input name="fTemp" type="text"/></td>
<td><button onclick="convertToC">Convert to Celsius</button></td>
</tr>
<tr>
<td>Celsius</td>
<td><input name="cTemp" type="text"/></td>
<td><button onclick="convertToF">Convert to Fahrenheit</button></td>
</tr>
</form>
</tbody>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
你有一些错误.请参阅此更正的jsFiddle示例.
document.getElementByIdJavaScript的
function convertToC() {
var fTempVal = parseFloat(document.getElementById('fTemp').value);
var cTempVal = (fTempVal - 32) * (5 / 9);
document.getElementById('cTemp').value = cTempVal;
return false;
}
function convertToF() {
var cTempVal = parseFloat(document.getElementById('cTemp').value);
var fTempVal = (cTempVal * (9 / 5)) + 32;
console.log(fTempVal);
document.getElementById('fTemp').value = fTempVal;
return false;
}
Run Code Online (Sandbox Code Playgroud)
HTML
<form name="conversionForm">
<table border="1">
<tbody>
<tr>
<td>Fahrenheit</td>
<td>
<input name="fTemp" id="fTemp" type="text" />
</td>
<td>
<button type="button" onclick="convertToC();return false">Convert to Celsius</button>
</td>
</tr>
<tr>
<td>Celsius</td>
<td>
<input name="cTemp" id="cTemp" type="text" />
</td>
<td>
<button type="button" onclick="convertToF();return false">Convert to Fahrenheit</button>
</td>
</tr>
</tbody>
</table>
</form>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23423 次 |
| 最近记录: |