【问题标题】:Switch Statement In Loop Logging Wrong Value循环中的 Switch 语句记录错误值
【发布时间】:2014-11-11 04:20:46
【问题描述】:

我有一个如下所示的 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;
  • 难道它不会在每个循环中重新分配吗?我想我不明白它为什么会那样做。如果仅假设在 case 为 '8' 时才运行,它为什么会打印 '3'?
  • 您的简化代码示例按原样工作,即使@bencripps 是正确的,您可能需要在每个案例之后中断。在 jsfiddle 中尝试一下,你会得到正确的输出。当你“简化”时,你一定遗漏了一些关键的东西。我们很勇敢,让我们看看剩下的 ;)
  • @bitfiddler,刚刚更新。你会看到我正在循环遍历表数据...
  • @bencripps 是对的。您需要在每个案例结束时有一个中断声明,否则它的处理将一直“落空”到下一个案例。

标签: javascript loops switch-statement


【解决方案1】:

您需要在每个 case 结束时使用 break 语句,否则处理将一直“落空”到下一个 case 直到结束。情况 8 中的打印将始终执行。

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2014-07-25
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    相关资源
    最近更新 更多