2011-05-06 55 views
0

我对JavaScript很陌生,有人帮我编写了这个脚本,它在Chrome上效果很好,但它在Firefox中不起作用,并且还没有在IE中测试过,但我希望它能够在所有浏览器,我真的不知道那的jQuery高达翻译它什么在这里混淆了我的跨浏览器功能?

function displayTotal() 
{ 

    var tableRows = document.getElementById('budgetTable').getElementsByTagName('tr'); 
    var totalDays = tableRows.length - 3; //Don't count the header rows and the Total rows 

    var totalPrice = 0; 

    var price = filterNum(document.getElementById('txtPrice').value); 
    var totalField = document.getElementById('txtTotal'); 


    var tempHours = 0; 
    var tempTotal = 0; 

    for(var i = 0; i < totalDays; i++) 
    { 

     tempHours = document.getElementById("end" + i).value - document.getElementById("start" + i).value; 
     tempTotal = tempHours * price; 

     document.getElementById("total" + i).innerHTML = formatCurrency(tempTotal); 
     totalPrice += tempTotal; 
     console.log(i, "Start:" + document.getElementById("start" + i).value, "End:" + document.getElementById("end" + i).value, "Hours:" + tempHours, "Total:" + tempTotal); 
    } 

    totalField.value = formatCurrency(totalPrice); 

} 

function addRowToTable() 
{ 
    var tbl = document.getElementById('budgetTable'); 
    var lastRow = tbl.rows.length - 2; 
    var iteration = lastRow; 
    var entry = iteration - 1; //because we started with day0, etc 
    var row = tbl.insertRow(lastRow); 

    // day cell 
    var cellDay = row.insertCell(0); 
    cellDay.appendChild(createInput('text','day' + entry, '', displayTotal)); 

    // start cell 
    var cellStart = row.insertCell(1); 
    cellStart.appendChild(createInput('text','start' + entry, 0, displayTotal)); 

    // end cell 
    var cellEnd = row.insertCell(2); 
    cellEnd.appendChild(createInput('text','end' + entry, 0, displayTotal)); 

    // total cell 
    var cellTotal = row.insertCell(3); 
    cellTotal.id = 'total' + entry; 

} 

function createInput(type, id, value, action) 
{ 
    var el = document.createElement('input'); 
    el.type = type; 
    el.id = id; 
    el.value = value; 
    el.onkeyup = action; 
    return el; 
} 

function filterNum(str) 
{ 
    re = /^\$|,/g; 
    // remove "$" and "," 
    return str.replace(re, ""); 
} 

function formatCurrency(num) 
{ 
    num = isNaN(num) || num === '' || num === null ? 0.00 : num; 
    return parseFloat(num).toFixed(2); 
} 

任何帮助将受到欢迎,因为我真的不知道我失去了再点这里。

编辑:好吧,这很奇怪,但在Firefox上,当我启用萤火虫试图调试它时,...它的工作原理。

+0

@ la_f0ka:您在Firefox中遇到什么错误?你之前用过萤火虫吗? – shane87 2011-05-06 14:30:50

+0

'totalDays = tableRows.length-3'是多余的。如果你想跳过'header'行,定义一个thead和tbody。然后,你可以将一个id放在tbody上并获取这些行或者getElementById('budgetTable')。getElementsByTagName(“tbody”)[0] .length – Gary 2011-05-06 14:36:26

+1

“It does not work”is * never * a good error description。请告诉出了什么问题,预期的行为是什么,以及如果您在控制台中收到错误消息。 – 2011-05-06 14:37:03

回答

0
tempHours = document.getElementById("end" + i).value - document.getElementById("start" + i).value; 
document.getElementById("total" + i).innerHTML 

输出任何元素属性(如element.value和element.innerHTML)始终是一个字符串。您只能对数字执行数学函数。如果字符串只包含'数字字符',您可以使用parseInt()或parseFloat将它们转换为

console.log(); // Is a Firebug function. Try commenting it out. 
+1

这不应该导致任何问题,因为JavaScript会在需要时自动转换类型。 – 2011-05-06 14:42:58

+1

我站好了;除此之外,它确实是唯一无法判断是否使用数字或字符串的情况。无论如何,将您的值转换为数字仍然是一个好习惯。 – Gary 2011-05-06 14:50:25