简体   繁体   English

即使使用parseInt转换计算后,我仍然会得到NaN

[英]I still get NaN even after converting calculations using parseInt

I Need to print the calculations gotten from options inside the Gross input box(3rd box) but instead of the calculation, I receive NaN. 我需要打印从“总值”输入框(第3个框)内的选项获得的计算结果,但我收到的是NaN而不是计算结果。

The answer should return gross pay calculations based on the function below. 答案应返回基于以下函数的工资总额计算。

If the hours worked are less than equal 40, the calculation will be inside the if condition, or else, a different calculation. 如果工作小时数小于等于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()"> 

You are assigning your inputs to uninitialized variables change those to this 您正在将输入分配给未初始化的变量,将其更改为此

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM