Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript计算器不是';行不通_Javascript_Jquery_Math_Nan - Fatal编程技术网

JavaScript计算器不是';行不通

JavaScript计算器不是';行不通,javascript,jquery,math,nan,Javascript,Jquery,Math,Nan,我的小时工资计算器坏了。我应该得到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

我的小时工资计算器坏了。我应该得到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\">&nbsp;</span> to earn his yearly income and <span id=\"your-time\">&nbsp;</span> for him to earn yours.");
});
var hourly=$('#txtHourlyWage').val();
var分数=小时/60/60/10;
var calc=新计算器();
函数addCommas(str){
返回(str+“”)。替换(/\b(\d+)(\。\d+*)\b/g,函数(a,b,c){
返回(b.charAt(0)>0&!(c | |“)).lastIndexOf(“.”)b.replace(/(\d)(?=(\d{3})+$)/g,“$1,”):b)+c;
});
}
$('year calculation').html(addCommas(Math.round(每年计算/(每小时*每小时计算)))+'years');
$(“#你的时间”).html(((每小时*2080)/计算年度)*52*5*8.toFixed(1)+“小时”);
$('#txtHourlyWage').bind('keypress',函数(e){
如果($('#txtHourlyWage')。长度<2){
返回(e.which!=8&&e.which!=0&&e.which57))?false:true;
}否则{
返回false;
}
返回
})
$('#txtHourlyWage').keyup(函数(){
$('year calculation').html(addCommas(Math.round(每年计算/(每小时*每小时计算)))+'years');
$(“#你的时间”).html(((每小时*2080)/计算年度)*52*5*8.toFixed(1)+“小时”);
$(“#HourlyWageOutput”).html(“以这种速度,你需要为他赚取年收入,他也需要为你赚取年收入。”);
});
此代码生成以下内容:

以这种速度,你需要为他赚取年收入,他也需要为你赚取年收入。
南小时


从上面可以看出,代码产生的内容没有意义,因为缺少数字。我不知道我做错了什么。请帮助。

您没有在
键控事件上设置数字

您需要将这些行:

$('#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");
键控器内
。事实上,它们只被发射一次,加载,大概在有任何计算之前

再次编辑:尚未测试,但我认为它应该可以工作:

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");
});
函数addCommas(str){
返回(str+“”)。替换(/\b(\d+)(\。\d+*)\b/g,函数(a,b,c){
返回(b.charAt(0)>0&!(c | |“)).lastIndexOf(“.”)b.replace(/(\d)(?=(\d{3})+$)/g,“$1,”):b)+c;
});
}
$('#txtHourlyWage').bind('keypress',函数(e){
如果($('#txtHourlyWage')。长度<2){
返回(e.which!=8&&e.which!=0&&e.which57))?false:true;
}否则{
返回false;
}
返回;
});
$(“#HourlyWageOutput”).html(“以这种速度,你需要为他赚取年收入,他也需要为你赚取年收入。”);
$('#txtHourlyWage').keyup(函数(){
var hourly=$(this.val();
var分数=小时/60/60/10;
var calc=新计算器();
$('year calculation').html(addCommas(Math.round(每年计算/(每小时*每小时计算)))+'years');
$(“#你的时间”).html(((每小时*2080)/计算年度)*52*5*8.toFixed(1)+“小时”);
});

当然,如果在HTML中已经定义了
$(“#hourlyvageoutput”)
,您不需要设置它。

从console.log()开始,查看所有变量和输出在哪里丢失值。是否有理由每次在
#txthourlyvageoutput
上获得密钥时都重置
#hourlyvageoutput
的HTML?那里的文本看起来非常静态,实际上可能正在重置span元素
年份计算
您的时间
,我肯定您不希望这样。我不熟悉console.log。如何使用console.log?@JaPerk14-
console.log(“Hello World”)
在浏览器控制台中输出
“Hello World”
console.log(window)
输出每一个属性。这
if($(''txtHourlyWage').length<2)
可能应该是
if(This.value.length<2)
,因为
$('.'txtHourlyWage').length
总是小于2,因为您正在测量jQuery对象中DOM项的数量,不是字段中文本的长度。我认为基本问题是您需要在
keyup
事件中进行所有这些计算。这包括抓取
hourly
变量,计算其分数,等等。在页面加载计算内容时,每次启动
keyup
时,它仍然引用那些原始变量。我还尝试了您的解决方案,但是仍然没有得到我想要的结果。我想你不会有什么好结果吧?代码编写得有点密集,以便于阅读。试着把所有不必要的东西都去掉,放在小提琴里,我们就从那里开始。