3

我在 JavaScript 中使用循环嵌套开关,例如:

for (var i = 0; i < checkBoxIds.length; i++) {
        if ($('#' + checkBoxIds[i]).prop('checked')) {
            var id = checkBoxIds[i];
            var assetCat = id.substring(0, 4);
            switch (id.substring(id.length - 3)) {
                case "scr":
                    if (!sscripts)
                        if (confirm("Name of scripts sub-folder (in shared) is not provided for " + assetCat + ". Press OK to Continue for others?"))
                            continue; else break; //else return
                    //Appending chrs or sets or props to scripts
                    switchAssets(sscripts, IdEnum.SCRIPTS);
                    break;
                case "shd":
                    if (!sshaders)
                        if (confirm("Name of shaders sub-folder (in shared) is not provided for " + assetCat + ". Press OK to Continue for others?"))
                            continue; else break; //else return
                    //Appending chrs or sets or props to shaders
                    switchAssets(sshaders, IdEnum.SHADERS);
                    break;
                case "sim":
                    if (!ssourceimages)
                        if (confirm("Name of sourceimages sub-folder (in shared) is not provided for " + assetCat + ". Press OK to Continue for others?"))
                            continue; else break; //else return
                    //Appending chrs or sets or props to sourceimages
                    switchAssets(ssourceimages, IdEnum.SOURCEIMAGES);
                    break;
                default:
            }
        }
    }

    //...Still doing something (else return; will never kiss this :D )
}

如果!sscripts是假的,我问用户是否要继续其他复选框,如果他取消,我想打破循环并在函数中执行剩余的语句。好像休息;在执行 switch 的确认对话框中,我怎样才能让它运行 for-loop。

任何建议将不胜感激。

4

2 回答 2

0

这正是标签的用途:

theloop:
  for (var i = 0; i < checkBoxIds.length; i++) {
    ...

    switch (id.substring(id.length - 3)) {

      ...
        break theloop;
        // continue theloop;
于 2013-09-16T05:45:52.650 回答
-1

而不是这个

 continue; else break; //else return

尝试这个

continue; else break iWantHere;//用标签打断

将此标签添加到iWantHere您希望控件去的位置

Example

 for(...)
 {
    switch('a')
    {
       case 'a': break iWantHere; // This will exit out of loop
       default:
    }
 }  

 iWantHere :
  // Rest of your code
于 2013-09-16T05:44:28.993 回答