简体   繁体   English

计算结果未显示在总计表中

[英]Calculation results not displaying in totals table javascript

I'm trying to create a paycheck calculator, but when I enter the hourly rate and number of hours and hit calculate, all I'm getting is undefined in my totals table. 我正在尝试创建一个薪水计算器,但是当我输入小时费率和小时数并点击计算时,我得到的全部都没有在总计表中定义 Here is my javascript: 这是我的JavaScript:

var $ = function (id) {
return document.getElementById(id); 
}

function updateTotal () {

var rate;
var hours = parseFloat( $("numHours").value);
var regularHours;
var overtime;
var doubletime;

    //begin determine hourly rate 
    function checkJob () {
    if ($('job').value == 'job0') {
    rate = 0;
    }
    if ($('job').value == 'job1') {
    rate = 12;
    }
    if ($('job').value == 'job2') {
    rate = 10;
    }
    if ($('job').value == 'job3') {
    rate = 11;
    }
    }

    //calculate hours breakdown
    function checkHours () {
    if (hours.value <= 40) {
    regularHours = hours.value;
    overtime = 0;
    doubletime = 0;
    }

    if (hours.value < 60) {
    regularHours = 40;
    overtime = hours.value-40;
    doubletime=0;
    }

    if (hours.value >=60) {
    regularHours = 40;
    overtime = 20;
    doubletime = hours.value-60;
    }
    }

    checkJob();
    checkHours();

    var salary = (regularHours * rate) + (overtime * (rate * 1.5)) + (doubletime * (rate * 2))



    //display amounts
    $('regularHours').innerHTML = regularHours;
    $('overtime').innerHTML = overtime;
    $('doubletime').innerHTML = doubletime;
    $('salary').innerHTML = "$ " + salary;
}

I'm sure I'm missing something silly, but I've been staring at this code for days and can't figure out what I'm doing wrong. 我确定我缺少一些愚蠢的东西,但是我一直盯着这段代码好几天了,无法弄清楚我在做什么错。 Thanks for looking! 感谢您的光临!

EDIT 编辑

The offending code is this: 令人讨厌的代码是这样的:

hours.value

The problem with it is that hours is already a number, and number does not have the method value available to it. 问题在于hours数已经是一个数字,并且数字没有可用的方法value Changing all instances of hours.value to hours will solve the problem and properly output a calculated result. hours.value所有实例更改为hours将解决问题并正确输出计算结果。

See this demo: http://jsfiddle.net/gLthv/ 观看此演示: http : //jsfiddle.net/gLthv/


The problem is the way you are implementing your jquery selectors. 问题是您实现jquery选择器的方式。 You should either use document.getElementById() instead, or remember that to select an id, you need to prefix the id name with a # 您应该改用 document.getElementById() ,或者记住要选择一个ID,您需要在ID名称前加上 #

 
 
 
  
  $("#id")
 
  

That is only the first part of the problem though. 不过,那只是问题的第一部分。 The next part is that you are using jquery, but expecting dom elements. 下一部分是您正在使用jquery,但需要dom元素。 To unwrap the jquery object, you need to use [0] with it to be able to access the contained dom element. 要解包jquery对象,您需要对其使用 [0]才能访问所包含的dom元素。

 
 
 
  
  $("#id")[0]
 
  

Once you make these changes, you should be able to see the reflected calculations. 进行这些更改后,您应该能够看到反映的计算结果。 For example, $('job').value should really be $('#job')[0].value or if you prefer to stick with jquery's implementation then you can use $('#job').val() to get the value. 例如, $('job').value实际上应该是 $('#job')[0].value或者如果您更喜欢使用jquery的实现,则可以使用 $('#job').val()获得价值。

Similar to this you may also access jquery's implementation of innerHTML . 与此类似,您也可以访问jquery的 innerHTML实现。 That would change this: $('overtime').innerHTML = overtime to $('#overtime').html(overtime) 那会改变: $('overtime').innerHTML = overtime$('#overtime').html(overtime)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM