简体   繁体   English

为什么我的 Javascript 变量没有初始化?

[英]Why is my Javascript variables are not initializing?

i am trying to build a simple Salary Calculator which deducts certain percentages from basic salary and then displays that value in that input element but it is not working and i am not sure what is wrong.我正在尝试构建一个简单的工资计算器,它从基本工资中扣除一定的百分比,然后在该输入元素中显示该值,但它不起作用,我不确定出了什么问题。 Maybe my variables are not initializing because after i run the program when i check for variable values in the console, it says that variable is undefined.也许我的变量没有初始化,因为在我运行程序后,当我在控制台中检查变量值时,它说该变量未定义。

during debugging the values were properly calculated but in the end not put into the input element.在调试期间,这些值被正确计算,但最终没有放入输入元素。 It may be a stupid mistake but i tried everything i could but can't seem to make it work.这可能是一个愚蠢的错误,但我尽我所能,但似乎无法让它发挥作用。 Any guidance would be helpful任何指导都会有所帮助

 function salCal() { var salary = document.forms[0][6].value; var utilities = document.forms[0][7].value; var houseRent = document.forms[0][8].value; var tax = document.forms[0][9].value; var totalSalary = document.forms[0][10].value; if (salary == "") { window.alert("Basic Salary Missing"); } utilities = 10 / 100 * salary; houseRent = 8 / 100 * salary; tax = 2 / 100 * salary; totalSalary = salary - (utilities + houseRent + tax); }
 <center> <h1>Employee Salary Calculator</h1> <form> <table> <tr> <td>Employee ID</td> <td> <input type="text" value=""></td> </tr> <tr> <td>Employee Name</td> <td><input type="text" value=""></td> </tr> <tr> <td>Father Name</td> <td><input type="text" value=""></td> </tr> <tr> <td>Gender</td> </tr> <tr> <td> Male </td> <td> <input type="radio" name="gender" checked> </td> </tr> <tr> <td> Female </td> <td> <input type="radio" name="gender"> </td> </tr> <tr> <td>CNIC</td> <td><input type="text" value=""></td> </tr> <tr> <td>Basic Salary*</td> <td><input type="number" name="sal"></td> </tr> <tr> <td>Utilities*</td> <td><input type="number" disabled></td> </tr> <tr> <td>House Rent*</td> <td><input type="number" disabled></td> </tr> <tr> <td>Tax Percentage*</td> <td><input type="number" disabled></td> </tr> <tr> <td>Total Salary*</td> <td><input type="number" disabled></td> </tr> <tr> <td>Tax Year</td> <td> <select> <option selected>2014</option> <option>2015</option> <option>2016</option> <option>2017</option> <option>2018</option> </select> </td> </tr> <tr> <td><input type="button" value="Calculate" onclick="salCal()"></td> <td><input type="button" value="Reset"></td> </tr> </table> </form> </center>

The scope of the variables are within the function salCal .变量的范围在函数salCal If you want it in console, put the variables outside in global scope如果您想在控制台中使用它,请将变量放在全局范围之外

var salary, utilities, houseRent , tax , totalSalary  ;

function salCal() {
    salary = document.forms[0][6].value;
    utilities = document.forms[0][7].value;
    houseRent = document.forms[0][8].value;
    tax = document.forms[0][9].value;
    totalSalary = document.forms[0][10].value;

    if(salary == "") {
        window.alert("Basic Salary Missing");
    }

    utilities = 10/100*salary;
    houseRent = 8/100*salary;
    tax = 2/100*salary;
    totalSalary = salary - (utilities + houseRent + tax);

}

during debugging the values were properly calculated but in the end not put into the input element在调试期间,值被正确计算,但最终没有放入输入元素

You need to assign the value back to input您需要将值分配回输入

document.forms[0][10].value = totalSalary ;

The reason is because your variables are within the scope of your salCal() function.原因是因为您的变量在您的salCal()函数的范围内。 These variables can be accessed within the function, but are deleted as soon as the function ends.这些变量可以在函数内访问,但在函数结束后立即删除。 To fix this, define your variables outside of the function (as a placeholder value), then give them actual value inside of the function.要解决此问题,请在函数外部定义变量(作为占位符值),然后在函数内部为它们提供实际值。

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

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