我的小时工资计算器无效。我应该得到3个不同的数字,但是两个第一个数字没有显示,最后一个数字是NaN。请帮忙。我正在使用的代码如下所示。
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小时
从上面可以看出,代码生成的内容没有意义,因为缺少数字。我不知道我做错了什么。请帮忙。
答案 0 :(得分:1)
您没有在keyup
事件中设置数字。
你需要把这些行:
$('#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");
在keyup
内。事实上,他们只被解雇了一次,大概是在有任何事情要计算之前。
再次编辑:仍然没有经过测试,但我认为它应该有效:
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;
});
}
$('#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;
});
$('#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.");
$('#txtHourlyWage').keyup(function (){
var hourly = $(this).val();
var fraction = hourly/60/60/10;
var calc = new Calculator();
$('#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");
});
当然,如果已经在HTML中定义了$('#HourlyWageOutput')
,则无需设置。{/ p>