2

作为序言,我们是一个小组织,这个系统是很久以前有人建立的。我是 javascript 的新手,所以我在做复杂的事情时遇到了麻烦,但我会尽力理解你的答案。但不幸的是,此时从头开始重做所有事情并不是一个真正的选择。

我们有一个收集数据的系统,客户使用登录来验证会员 ID,然后系统使用该系统将记录从 MS Access 数据库中提取到 .ASP/html 表单,以便客户可以更新他们的数据。其中一个页面具有在表单提交上运行的以下功能,以检查字段 a/b/c 中的数据总和是否与 d/e/f/g/h/i 相同。它对显示的每一列分别执行此操作(每一列是数据库中的一条记录,每个 a/b/c/d/e/f 是记录中的一个字段。)

问题在于函数的这一部分:


for (var j=0; j<recCnt; j++) {
   sumByType = milesSurf[j] + milesElev[j] + milesUnder[j];
   sumByTrack = milesSingle[j] + milesDouble[j] + milesTriple[j] + milesQuad[j] + milesPent[j] + milesSex[j];

等等


它应该使用 javascript FOR 循环遍历每条记录并测试它们是否总和相同。

在 Firefox 和 IE 中,这可以正常工作;这些字段正确地汇总为“sumByType”和“sumByTrack”。您可以在下面看到我添加了一个小警报以找出问题所在:

alert(sumByType + " " + j + " " + recCnt + " " + milesSurf[j] + " " + milesElev[j] + " " + milesUnder[j]);

在 Chrome 中,该警报告诉我“sumByType”和“sumByTrack”(各种“milesXXXXX”变量)的组件未定义。

我的问题是:为什么在 Chrome 中它不能正常工作,而在 IE 和 FFox 中它是?有任何想法吗?

完整功能代码如下:


function submitCheck(formy, recCnt) {
//2/10/03: added milesQuad
//---------------checks Q#4 that Line Mileage by type is the same as by track     
var milesElev = new Array(); 
var milesSurf = new Array();
var milesUnder = new Array(); 
var milesSingle = new Array();
var milesDouble = new Array(); 
var milesTriple = new Array();
var milesQuad = new Array();
var milesPent = new Array();
var milesSex = new Array();
var sumByType = 0;
var milesLineTrack = new Array();   //this is for Q5 to compare it to mileage by trackage
var j = 0; var sumByTrack = 0; var liney; var yrOp;
//var str = "document.frm.milesElev" + j;
//alert(str.value);
for (var i in document.frm) {
    if (i.substring(0, i.length - 1) == "milesElev") {
        milesElev[parseInt(i.substring(i.length-1, i.length))] = parseFloat(document.frm[i].value); }   
    if (i.substring(0, i.length - 1) == "milesSurf") {
        milesSurf[parseInt(i.substring(i.length-1, i.length))] = parseFloat(document.frm[i].value); }
    if (i.substring(0, i.length - 1) == "milesUnder") {
        milesUnder[parseInt(i.substring(i.length-1, i.length))] = parseFloat(document.frm[i].value); }
    if (i.substring(0, i.length - 1) == "milesSingle") {
        milesSingle[parseInt(i.substring(i.length-1, i.length))] = parseFloat(document.frm[i].value); }
    if (i.substring(0, i.length - 1) == "milesDouble") {
        milesDouble[parseInt(i.substring(i.length-1, i.length))] = parseFloat(document.frm[i].value); }
    if (i.substring(0, i.length - 1) == "milesTriple") {
        milesTriple[parseInt(i.substring(i.length-1, i.length))] = parseFloat(document.frm[i].value); }
    if (i.substring(0, i.length - 1) == "milesQuad") {
        milesQuad[parseInt(i.substring(i.length-1, i.length))] = parseFloat(document.frm[i].value); }
    if (i.substring(0, i.length - 1) == "milesPent") {
        milesPent[parseInt(i.substring(i.length-1, i.length))] = parseFloat(document.frm[i].value); }
    if (i.substring(0, i.length - 1) == "milesSex") {
        milesSex[parseInt(i.substring(i.length-1, i.length))] = parseFloat(document.frm[i].value); }
    if (i.substring(0, i.length -1) == "milesLineTrack") {
        milesLineTrack[parseInt(i.substring(i.length-1, i.length))] = document.frm[i].value; }  //12/13/02 used to be parseFloat(document.frm[i].value)
    if (i.substring(0,5)=="Lines") {
        liney = document.frm[i].value;
    if (parseInt(liney)<1 || isNaN(liney)) {
        alert("Each mode must have at least 1 line.  Please correct the value in question #2."); 
        document.frm[i].select(); return false; }}
    if (i.substring(0,8)=="yearOpen") { 
        yrOp = document.frm[i].value;
        if (parseInt(yrOp)<1825 || isNaN(yrOp)) {
            alert("Please enter a year after 1825 for question #3"); 
            document.frm[i].select(); return false; }
    }
}
for (var j=0; j<recCnt; j++) {
    sumByType = milesSurf[j] + milesElev[j] + milesUnder[j];
    sumByTrack = milesSingle[j] + milesDouble[j] + milesTriple[j] + milesQuad[j] + milesPent[j] + milesSex[j];

    //---------------to round sumByTrack and sumByType from a long decimal to a single decimal place, like frm 7.89999998 to 7.9.
    sumByTrack = sumByTrack * 10;
    if (sumByTrack != parseInt(sumByTrack)) {
        if (sumByTrack - parseInt(sumByTrack) >= .5) {
        //round up
            sumByTrack = parseInt(sumByTrack) + 1; }
        else {  //truncate
            sumByTrack = parseInt(sumByTrack); }}
    sumByTrack = sumByTrack / 10;
    sumByType = sumByType * 10;
    if (sumByType != parseInt(sumByType)) { 
        if (sumByType - parseInt(sumByType) >= .5) {
        //round up
            sumByType = parseInt(sumByType) + 1; }
        else {  //truncate
            sumByType = parseInt(sumByType); }}
    sumByType = sumByType / 10;
    //-------------end of rounding ---------------------------

    if (sumByType != sumByTrack) {
        if (isNaN(sumByType)) { 
            sumByType = "(sum of 4.a., b., and c.) "; }
        else {
            sumByType = "of " + sumByType; }
        if (isNaN(sumByTrack)) { 
            sumByTrack = "(sum of 4.d., e., f., g., h., and i.) "; }
        else {
            sumByTrack = "of " + sumByTrack; }
        alert("For #4, the 'End-to-End Mileage By Type' " + sumByType + " must equal the 'End-to-end Mileage By Trackage' " + sumByTrack + ".");
        alert(sumByType + " " + j + " " + recCnt + " " + milesSurf[j] + " " + milesElev[j] + " " + milesUnder[j]);
        return false;
    }
    //alert (milesLineTrack[j] + "  " + milesSingle[j] + "  " + 2*milesDouble[j] + "  " + 3*milesTriple[j] + "  " + 4*milesQuad[j] + "  " + 5*milesPent[j] + "  " + 6*milesSex[j]);
    var singDoubTrip = (milesSingle[j] + 2*milesDouble[j] + 3*milesTriple[j] + 4*milesQuad[j] + 5*milesPent[j] + 6*milesSex[j])
    //----------round singDoubTrip to one digit after the decimal point (like from 6.000000001 to 6.0)
    singDoubTrip = singDoubTrip * 10;
    if (singDoubTrip != parseInt(singDoubTrip)) { 
        if (singDoubTrip - parseInt(singDoubTrip) >= .5) {
        //round up
            singDoubTrip = parseInt(singDoubTrip) + 1; }
        else {  //truncate
            singDoubTrip = parseInt(singDoubTrip); }}
    singDoubTrip = singDoubTrip / 10;
    //----------end round singDoubTrip-----------------------------------------
    if (parseFloat(milesLineTrack[j]) != singDoubTrip) {
        //var mlt = milesLineTrack[j];
        //if isNaN(milesLineTrack[j]) { mlt = 
        alert("For column #" + (j+1) + ", the mainline passenger track mileage of " + milesLineTrack[j] + " must equal the single track plus 2 times the double track plus 3 times the triple track plus 4 times the quadruple track plus 5 times the quintuple track plus 6 times the sextuple track, which is " + singDoubTrip + ".");
        return false; 
    }
}
//---------------------end of checking Q#4----------------

//return false;

}


4

1 回答 1

3

我认为for (var i in document.frm)是问题所在。你不应该枚举一个form元素,会有很多意想不到的属性 - 请参阅为什么将“for...in”与数组迭代一起使用是一个坏主意?,对于类似数组的对象尤其如此。我不敢相信这在 FF 中可以正常工作:-)

用这个:

var ele = document.frm.elements; // or even better document.getElementById("frm")
for (var i=0; i<ele.length; i++) {
    // use ele[i] to access the element,
    // and ele[i].name instead of i where you need the name
}

此外,您应该喜欢循环而不是那些大量的 if 语句。

于 2013-01-30T14:09:06.280 回答