什么在这里弄乱我的跨浏览器功能?

时间:2011-05-06 14:20:18

标签: javascript cross-browser

我是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上,当我启用firebug尝试调试它时,......它可以工作。

1 个答案:

答案 0 :(得分: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.