我懒洋洋地硬编码了工作的数量,但这就是你想要的。
<script type="text/javascript">
function checkHours() {
totalHours = Number(form.total.value);
realTotal = 0;
for (x = 1; x <= 4 /* hard-coded job count */; x++)
realTotal += Number(eval("form.job" + x + ".value"));
if (totalHours != realTotal) {
alert("Total hours does not match cumulative job hours");
return false;
}
}
</script>
<form name="form" method="post" onsubmit="return checkHours()">
<table>
<tr>
<td>Total Hours</td><td><input type="text" name="total"/></td>
</tr>
<tr>
<td>Job 1</td><td><input type="text" name="job1"/></td>
</tr>
<tr>
<td>Job 2</td><td><input type="text" name="job2"/></td>
</tr>
<tr>
<td>Job 3</td><td><input type="text" name="job3"/></td>
</tr>
<tr>
<td>Job 4</td><td><input type="text" name="job4"/></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
If this is in case of mistakes: my solution will suffice. If it's because of highly untrustworthy employees that would intentionally attempt to fake hours: Bubbles is right and you should check it with php as well.