2012-10-15 71 views
0

我的小时工资计算器不起作用。我应该得到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\">&nbsp;</span> to earn his yearly income and <span id=\"your-time\">&nbsp;</span> for him to earn yours."); 
}); 

此代码将产生以下:

按照这种速度,它会带你 赢得他的年收入和 为他赚你的。
的NaN小时

正如你可以从上面看到,是什么代码已经产生,因为数字是缺少没有意义。我不知道我做错了什么。请帮忙。

+3

从console.log()开始,查看所有变量和输出以查看丢失值的位置。 –

+0

每当你在'#txtHourlyWage'上加密时,你是否有理由重置'#HourlyWageOutput'的HTML?那里的文字看起来非常静态,它实际上可能会重置span元素'year-calculation'和'your-time',我相信你不需要。 –

+0

我对console.log不熟悉。我将如何使用console.log? – JaPerk14

回答

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\">&nbsp;</span> to earn his yearly income and <span id=\"your-time\">&nbsp;</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"); 
}); 

当然,你并不需要设置$('#HourlyWageOutput')如果它在HTML中已经定义。

+0

我编辑了代码,但我没有收到任何东西。 – JaPerk14

+0

我认为最基本的问题是你需要在'keyup'事件中做所有这些计算。这包括获取'hourly'变量,计算它的分数等。在网页加载时计算东西,每次'keyup'被触发时,它仍然会引用那些原始变量。 –

+0

我也试过你的解决方案,但仍然没有得到我正在寻找的输出。 – JaPerk14