Javascript函数无法在firefox或safari中运行

时间:2014-02-19 10:49:49

标签: javascript

我试图在页面底部显示交易总和。

function doTotal() {
    var Stuff = document.getElementsByTagName("input");
    var theTotal = new Number(0);
    for (var i = 0; i < Stuff.length; i++) {
        if (Stuff[i].getAttribute('type') == 'text') {
            if ((Stuff[i].value != '') && (IsNumeric(Stuff[i].value) == true) && (Stuff[i].name.substr(0, 8) == 'txtValue')) {
                theTotal = theTotal + parseFloat(Stuff[i].value);
            }
        }
    }
    document.getElementById("tdTotal").innerHTML = "R " + theTotal.toFixed(2);
    frm.txtTotal.value = theTotal.toFixed(2);
    //alert(theTotal);
}

1 个答案:

答案 0 :(得分:0)

修改

Ofc Im愚蠢,它无法工作,因为输入的值总是字符串。所以我改变了条件。现在它应该工作:

function doTotal() {
    var stuff = document.getElementsByTagName("input");
    var theTotal = 0;
    for (var i = 0; i < stuff.length; i++) {
        if (stuff[i].getAttribute('type') == 'text') {
            if ((stuff[i].value != '') && !isNaN(stuff[i].value) && (typeof stuff[i].name.substr(0, 8) === "string")) {
                theTotal += parseFloat(stuff[i].value);
            }
        }
    }
    // document.getElementById("tdTotal").innerHTML = "R " + theTotal.toFixed(2);
    // frm.txtTotal.value = theTotal.toFixed(2);
    alert(theTotal);
}

在那里试试:http://jsfiddle.net/windkiller/9dvRS/

<强> EDIT2:

调试它,这样你就可以看出哪些条件没有错误:

function doTotal(){         var stuff = document.getElementsByTagName(“input”);         var theTotal = 0;

        var i = 0;

        alert(stuff[i].getAttribute('type') == 'text');
        alert(stuff[i].value != '');
        alert(!isNaN(stuff[i].value));
        alert(typeof stuff[i].name.substr(0, 8) === "string");
}