循环中的Switch语句记录错误的值

Switch Statement In Loop Logging Wrong Value

本文关键字:错误 记录 Switch 循环 语句      更新时间:2023-09-26

我有一个switch语句,如下所示:

subCatTDs = 8;
switch(subCatTDs){
    case 3:
        //Do some code
    case 2:
        //Do some code
    case 8:
        console.log(subCatTDs);
}

我从代码的其余部分简化了这一点,试图了解正在发生的问题。subCatTDs确实被设置为不同的值(并且是一个更大循环的一部分),但在这种情况下,你会认为我在switch语句之前明确声明了它的值,控制台正在打印(正如我所说,它处于一个循环中):

3
8

当我直接在语句之前设置值时,为什么它会说"3"?

编辑

以下是更多的代码。对于更宏大的方案来说,这可能没有太大意义,但它可能会有所帮助(这是一个非常复杂的插件,所以很难在这里包含所有内容;如果你需要澄清什么,请告诉我):

for(i = 0; i < numberTRs; i++){
    var subCatValue = i + 1;
    var subCatRow = $('.subCatTable > tbody > tr:nth-child(' + subCatValue + ')');
    var subCatTDs = subCatRow.find('> td').length;
    //This used to be 5 now it's 4?
    switch(subCatTDs){
        case 3:
            //Subcat Title
            for(j = 0; j < subCatTDs; j++){
                    switch(j){
                    case 0:
                    case 2:
                        var nthChild = j + 1;
                        var subCatElement = $('.subCatTable tr:nth-child(' + subCatValue + ') td:nth-child(' + nthChild + ')');
                        var subCatTitle = subCatElement.html();
                        $('div#subcat-' + rowCounter + '-' + subCatCounter).append('<div class="subCatTitle">' + subCatTitle + '</div>');
                        subCatCounter++;
                        if(subCatCounter>subCatPerRow){
                            subCatCounter = 1;
                        }
                    }
            }
        case 2:
            //First time through
            //Second time through
            if(case2==true){
                        rowCounter++;
                        case2 = false;
                        continue;
            }
            case2 = true;
        case 8:
            console.log(subCatValue);
            for(j = 0; j < subCatTDs; j++){
                        switch(j){
                            case 0:
                            case 5:
                                        //Get the appropriate nth-child
                            var nthChild = j + 1;
                            var subCatElement = $('.subCatTable tr:nth-child(' + subCatValue + ') td:nth-child(' + nthChild + ')');
                            var subCatImage = subCatElement.html();
                                            $('div#subcat-' + rowCounter + '-' + subCatCounter).append('<div class="subCatImage">' + subCatTitle + '</div>');
                                            subCatCounter++;
                        if(subCatCounter>subCatPerRow){
                                                subCatCounter = 1;
                            }
                            case 3:
                            case 8:
                        var subCatDescrip = subCatElement.html();
                        $('div#subcat-' + rowCounter + '-' + subCatCounter).append('<div class="subCatImage">' + subCatDescrip + '</div>');
                        subCatCounter++;
                        if(subCatCounter>subCatPerRow){
                                                subCatCounter = 1;
                            }
                 }
            }               
        }
    }

编辑器上的格式设置根本无法正常工作。一切都不协调,但我想我已经接近了。不确定这是否有意义,但你可以看到,每次它在for循环中循环时,我都会重置subCatTD。它永远不应该打印出"3",因为我只是在检查了8的大小写后才使用console.log(),但出于某种原因,它确实打印了。

任何帮助都会很棒。谢谢

在每个案例的末尾都需要一个break语句,否则处理将一直"失败"到下一个案例。情况8中的打印将始终执行。