这是我的脚本:
<script>
jQuery(document).ready(function () {
jQuery('#btnCalculate').click(function () {
var salaries = parseInt(jQuery('#txtEmployeeSalaries').val(), 10);
var nationalInsurance = parseInt(jQuery('#txtENIC').val(), 10);
var pensionCont = parseInt(jQuery('#txtEPC').val(), 10);
var expenses = parseInt(jQuery('#txtAnyExpenses').val(), 10);
var income = parseInt(jQuery('#txtIncome').val(), 10);
var labourCost = (((salaries + nationalInsurance + pensionCont + expenses) / (income)) * 100);
alert(labourCost);
jQuery('#txtTotal').val(labourCost).toFixed(2);
});
});
</script>
但是在 Chrome 控制台中它指出:
未捕获类型错误:对象 [object Object] 没有方法“toFixed”
有人发现这有什么明显的问题吗?
最佳答案
以这种方式使用 toFixed
- (由于您尝试在 jquery 对象上使用该方法,因此会收到该错误)
jQuery('#txtTotal').val(labourCost.toFixed(2));
关于JQuery ToFixed 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16789824/