For Loop无法正常运行javascript

时间:2015-08-13 04:08:15

标签: javascript

vesselCount为2时,代码仅在未再次进入循环时才执行

function showAllProducts(type, vesselIndex, prodCount,vesselCount) {
    confirm (type+"==="+vesselIndex+"===="+prodCount);

    for(i=0;i<vesselCount;i++) {
        confirm("outerfor"+vesselCount);
        vesselIndex=vesselIndex+i;
    var tableId = "";
    if(type=="regular") {
        tableId = "productsTable"+vesselIndex;
        confirm ("productsTable"+vesselIndex);
    } else {
        tableId = "productsTableCompV"+vesselIndex;
    }
    var productsTable = document.getElementById(tableId);
    var prodCount = productsTable.rows.length-1;
    confirm("productcount"+prodCount);
    if(prodCount>0) {
        var expandUp = "";
        var expandDown = "";
        if(type == 'regular') {
            expandUp = "productExpandUp";
            expandDown = "productExpandDown";
        } else {
            expandUp = "compProductExpandUp";
            expandDown = "compProductExpandDown";
        }

        for(i=0;i<prodCount;i++) {
            var div1 = expandDown+vesselIndex+i;
            var div2 = expandUp+vesselIndex+i;
            showProductDetails(type, vesselIndex, i, div1, div2);
            confirm("fordone");
        }
        }

    }
}

...

function showProductDetails(type, vesselIndex, prodIndex, div1, div2){
    var applContId = "";
    if(type == 'regular') {
        applContId = "productApplCont";
    } else {
        applContId = "compProductApplCont";
    }
    document.getElementById(applContId+vesselIndex+prodIndex).style.visibility = "visible";
    document.getElementById(applContId+vesselIndex+prodIndex).style.display = "block";
    var jobType = document.getElementById("jobType").value;
    if(jobType=='INSP' || jobType=='MAR') {
    if(type == 'regular') {
    document.getElementById("productQuant"+vesselIndex+prodIndex).style.visibility = "visible";
    document.getElementById("productQuant"+vesselIndex+prodIndex).style.display = "block";
    }
    }
    document.getElementById(div1).style.visibility = "visible"; 
    document.getElementById(div2).style.visibility = "hidden";
}

4 个答案:

答案 0 :(得分:4)

你的内部for循环使用与外部for循环相同的变量(i)。将其更改为另一个变量。

for(var j = 0; j < prodCount; j++) { }

答案 1 :(得分:1)

您正在顶部for循环

中的内部for循环中重用i变量
function showAllProducts(type, vesselIndex, prodCount, vesselCount) {
    confirm(type + "===" + vesselIndex + "====" + prodCount);

    for (i = 0; i < vesselCount; i++) {
        if (prodCount > 0) {
            for (i = 0; i < prodCount; i++) { // <===========
            }
        }

    }
}

答案 2 :(得分:1)

你有2个for循环,

第4行for(i=0;i<vesselCount;i++) 和第29行for(i=0;i<vesselCount;i++) 这两个循环使用相同的迭代变量i,第二个循环中的i将覆盖第一个循环中的值。

在第二个循环中选择一个新的迭代变量应修复错误:

function showAllProducts(type, vesselIndex, prodCount, vesselCount) {
    confirm(type + "===" + vesselIndex + "====" + prodCount);

    for (var i = 0; i < vesselCount; i++) {
        confirm("outerfor" + vesselCount);
        vesselIndex = vesselIndex + i;
        var tableId = "";
        if (type == "regular") {
            tableId = "productsTable" + vesselIndex;
            confirm("productsTable" + vesselIndex);
        } else {
            tableId = "productsTableCompV" + vesselIndex;
        }
        var productsTable = document.getElementById(tableId);
        var prodCount = productsTable.rows.length - 1;
        confirm("productcount" + prodCount);
        if (prodCount > 0) {
            var expandUp = "";
            var expandDown = "";
            if (type == 'regular') {
                expandUp = "productExpandUp";
                expandDown = "productExpandDown";
            } else {
                expandUp = "compProductExpandUp";
                expandDown = "compProductExpandDown";
            }

            for (var j = 0; j < prodCount; j++) {
                var div1 = expandDown + vesselIndex + j;
                var div2 = expandUp + vesselIndex + j;
                showProductDetails(type, vesselIndex, j, div1, div2);
                confirm("fordone");
            }
        }

    }
}

答案 3 :(得分:0)

您正在为循环使用相同的迭代器

for(i=0;i<vesselCount;i++){
    confirm("outerfor"+vesselCount);
    vesselIndex=vesselIndex+i;
    ...
       for(i=0;i<prodCount;i++){
        var div1 = expandDown+vesselIndex+i;
        var div2 = expandUp+vesselIndex+i;
        showProductDetails(type, vesselIndex, i, div1, div2);
        confirm("fordone");
    }
    ...
} 

请更改内部循环迭代器变量

for(j=0;j<prodCount;j++)