我需要打印从总输入框(第3个框)中的选项中获得的计算,但是我没有计算,而是接收NaN。
答案应根据以下功能返回总薪酬计算。
如果工作小时数小于等于40,则计算将在if条件内,否则,计算结果不同。
function myFunction() {
var hoursWorked;
var hourlyRate;
document.getElementById("hours").value = hoursWorked;
document.getElementById("payrate").value = hourlyRate;
if (hoursWorked <= 40) {
var grossPay = parseInt((hoursWorked * hourlyRate)).toFixed(2);
document.getElementById("gross").value = grossPay;
} else {
var grossPay = parseInt((40 * hourlyRate + 1.5 * hourlyRate * (hoursWorked - 40))).toFixed(2);
document.getElementById("gross").value = grossPay;
}
}
# Add A H1 Heading Of "Gross Pay Calculation"
<form>
Hourly Rate:
<select id="payrate" align="right">
<script>
for(var d=10;d<=60;d++) {
document.write("<option>"+(d)+"</option>");
d+=9;
document.getElementById("payrate").value;
}
</script>
</select><br> Hours Worked:
<select id="hours" align="right">
<script>
for(var d=10;d<=60;d++) {
document.write("<option>"+d+"</option>");
d+=4;
document.getElementById("hours").value;
}
</script>
</select><br>
</form>
Gross Pay: <input type="text" id="gross" align="right"><br><br>
<input type="submit" value="GrossPay" onclick="myFunction()">
答案 0 :(得分:1)
您将输入分配给未初始化的变量,将其更改为
hoursWorked=document.getElementById("hours").value;
hourlyRate=document.getElementById("payrate").value;
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script>
function myFunction()
{
var hoursWorked;
var hourlyRate;
//hoursWorked=
hoursWorked=document.getElementById("hours").value;
hourlyRate=document.getElementById("payrate").value;
if (hoursWorked <=40)
{
var grossPay=parseInt((hoursWorked*hourlyRate)).toFixed(2);
document.getElementById("gross").value = grossPay;
}
else
{
var grossPay=parseInt((40*hourlyRate+1.5*hourlyRate*(hoursWorked-40))).toFixed(2);
document.getElementById("gross").value = grossPay;
}
}
</script>
</head>
<body>
# Add A H1 Heading Of "Gross Pay Calculation"
<form>
Hourly Rate: <select id="payrate" align="right">
<script>
for(var d=10;d<=60;d++)
{
document.write("<option>"+(d)+"</option>");
d+=9;
document.getElementById("payrate").value;
}
</script>
</select><br>
Hours Worked: <select id="hours" align="right">
<script>
for(var d=10;d<=60;d++)
{
document.write("<option>"+d+"</option>");
d+=4;
document.getElementById("hours").value;
}
</script>
</select><br>
</form>
Gross Pay: <input type="text" id="gross" align="right"><br><br>
<input type="submit" value="GrossPay" onclick="myFunction()">
</body>
</html>