我是HTML与JavaScript结合的新手。这是我的功课和我的代码有一些问题需要专家来看看。这是个问题。
如果工作时间大于40小时,则使用1.5倍的常规费率计算额外时间。例如,如果一个工作50小时并且支付的费率是10.00美元/小时,则总工资应为550.00美元。
我的代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function calc() {
var employ_name = document.getElementById('employ_name').value;
var rate_pay = parseInt(document.getElementById('rate_pay').value);
var hour_work = parseInt(document.getElementById('hour_work').value);
if (hour_work <= 40) {
alert("Paid Amount For " + employ_name + " is RM " + rate_pay * hour_work);
} else {
alert((40 * 1.5) + (c - 40) * c * 1.5);
}
}
function calc() {
if (document.getElementById("employ_name").value == "") {
alert("PLEASE ENTER THE VALUE!");
return false;
} else if (document.getElementById("employ_no").value == "") {
alert("PLEASE ENTER THE VALUE!");
return false;
} else if (document.getElementById("rate_pay").value == "" {
alert("PLEASE ENTER THE VALUE!");
return false;
} else {
alert("PLEASE ENTER THE VALUE!");
return false;
}
}
</script>
<title></title>
</head>
<body>
<form action="url" METHOD="POST">
<table>
<tr>
<td>Employee Name :</td>
<td><input type="text" id="employ_name"></td>
</tr>
<tr>
<td>Employee No :</td>
<td><input type="text" id="employ_no"></td>
</tr>
<tr>
<td>Rate of Pay :</td>
<td><input type="text" id="rate_pay"></td>
</tr>
<tr>
<td>Hours Work :</td>
<td><input type="text" id="hour_work"></td>
</tr>
<tr>
<td><input type="button" id="Button" value="Submit" onclick="calc()"></td>
</tr>
</table>
</form>
</body>
</html>
您的代码存在一些问题:
if
语句中添加结束括号来解决此问题:
(document.getElementById("rate_pay").value == "") {...}
checkInputs
,这样当你调用calc
函数时,它会调用预期的函数。calc
失败,你的第二个checkInputs
函数(false
)不应该返回if-statments
。相反,你应该返回true
,如果他们都fail
,因为这意味着他们都不是空的。calc
函数检查你的所有输入都有一个值,你可以在你的第一个calc
函数中调用你的第二个checkInputs
函数(现在calc
)。这样你就可以使用if
语句检查checkInputs
是否返回,从而采取相应的行动。请参阅以下修订代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function calc() {
if (checkInputs()) { // check if `checkInputs` returned true or false
var employ_name = document.getElementById('employ_name').value;
var rate_pay = parseInt(document.getElementById('rate_pay').value);
var hour_work = parseInt(document.getElementById('hour_work').value);
if (hour_work <= 40) {
alert("Paid Amount For " + employ_name + " is RM " + rate_pay * hour_work);
} else {
// commented out as you have not show us what "c" is:
// alert((40 * 1.5) + (c - 40) * c * 1.5);
}
} else { // if check inputs returned false then you can output error message
alert("PLEASE ENTER THE VALUE!");
}
}
function checkInputs() {
if (document.getElementById("employ_name").value.trim() == "") {
return false;
} else if (document.getElementById("employ_no").value.trim() == "") {
return false;
} else if (document.getElementById("rate_pay").value.trim() == "") {
return false;
}
return true;
}
</script>
<title>Calculator</title>
</head>
<body>
<form action="url" method="POST">
<table>
<tr>
<td>Employee Name :</td>
<td><input type="text" id="employ_name"></td>
</tr>
<tr>
<td>Employee No :</td>
<td><input type="text" id="employ_no"></td>
</tr>
<tr>
<td>Rate of Pay :</td>
<td><input type="text" id="rate_pay"></td>
</tr>
<tr>
<td>Hours Work :</td>
<td><input type="text" id="hour_work"></td>
</tr>
<tr>
<td><input type="button" id="Button" value="Submit" onclick="calc()"></td>
</tr>
</table>
</form>
</body>
</html>
注意:在你的checkInputs()
方法中,我也使用过.trim()
。这将删除输入上的前导/尾随空格。这意味着如果有人要进入空间,那么checkInputts
将返回false
而不是true
。