我的小时工资计算器不起作用。我应该得到3个不同的号码,但是两个第一个号码没有出现,最后一个号码是NaN。请帮忙。我使用的代码如下所示。JavaScript计算器不起作用
var hourly = $('#txtHourlyWage').val();
var fraction = hourly/60/60/10;
var calc = new Calculator();
function addCommas(str){
return(str+"").replace(/\b(\d+)((\.\d+)*)\b/g,function(a,b,c){
return(b.charAt(0)>0&&!(c||".").lastIndexOf(".")?b.replace(/(\d)(?=(\d{3})+$)/g,"$1,"):b)+c;
});
}
$('#year-calculation').html(addCommas(Math.round(calc.annual/(hourly*calc.per_hour))) + ' years');
$('#your-time').html((((hourly*2080)/ calc.annual) * 52 * 5 * 8).toFixed(1) + " hours");
$('#txtHourlyWage').bind('keypress', function(e) {
if ($('#txtHourlyWage').length < 2) {
return (e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
} else {
return false;
}
return
})
$('#txtHourlyWage').keyup(function(){
$('#year-calculation').html(addCommas(Math.round(calc.annual/(hourly*calc.per_hour))) + ' years');
$('#your-time').html((((hourly*2080)/ calc.annual) * 52 * 5 * 8).toFixed(1) + " hours");
$('#HourlyWageOutput').html("At this rate, it would take you <span id=\"year-calculation\"> </span> to earn his yearly income and <span id=\"your-time\"> </span> for him to earn yours.");
});
此代码将产生以下:
按照这种速度,它会带你 赢得他的年收入和 为他赚你的。
的NaN小时
正如你可以从上面看到,是什么代码已经产生,因为数字是缺少没有意义。我不知道我做错了什么。请帮忙。
从console.log()开始,查看所有变量和输出以查看丢失值的位置。 –
每当你在'#txtHourlyWage'上加密时,你是否有理由重置'#HourlyWageOutput'的HTML?那里的文字看起来非常静态,它实际上可能会重置span元素'year-calculation'和'your-time',我相信你不需要。 –
我对console.log不熟悉。我将如何使用console.log? – JaPerk14