2011-05-06 52 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