在线计算h值:使用公式h=√(r2-d2/4)-r
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>计算h值</title>
<style>
table {
border-collapse: collapse;
margin: 20px;
}
th, td {
border: 1px solid black;
padding: 10px;
}
input[type='number'] {
background-color: #F5DEB3;
border: none;
padding: 5px;
width: 100%;
box-sizing: border-box;
}
</style>
</head>
<body>
<h1>计算h值</h1>
<table>
<thead>
<tr>
<th>编号</th>
<th>r</th>
<th>d</th>
<th>h</th>
</tr>
</thead>
<tbody>
<!--循环100次-->
<?php for ($i = 1; $i <= 100; $i++) { ?>
<tr>
<td><?php echo $i; ?></td>
<!--定义r输入框-->
<td><input type='number' name='r[]' onkeyup='calculateH(this)' /></td>
<!--定义d输入框-->
<td><input type='number' name='d[]' onkeyup='calculateH(this)' /></td>
<!--定义h输出框-->
<td><input type='text' name='h[]' readonly /></td>
</tr>
<?php } ?>
</tbody>
</table>
<pre><code><script>
//计算h值
function calculateH(input) {
//获取当前行
var tr = input.parentNode.parentNode;
//获取r和d的值
var r = tr.querySelector('[name='r[]']').value;
var d = tr.querySelector('[name='d[]']').value;
//计算h的值
var h = Math.sqrt(Math.pow(r, 2) - Math.pow(d, 2) / 4) - r;
//将计算结果赋值给h输入框
tr.querySelector('[name='h[]']').value = h.toFixed(2);
}
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/m2JO 著作权归作者所有。请勿转载和采集!