Javascript 有条件的切换案例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2696436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:29:35  来源:igfitidea点击:

Switch case with conditions

javascriptjqueryswitch-statement

提问by Jasl

Am I writing the correct switch case?

我写的是正确的开关盒吗?

var cnt = $("#div1 p").length;
                alert(cnt);
                switch (cnt) {
                    case (cnt >= 10 && cnt <= 20):
                        alert('10');
                        break;
                    case (cnt >= 21 && cnt <= 30):
                       alert('21');
                        break;
                    case (cnt >= 31 && cnt <= 40):
               alert('31');
                        break;
                    default:
                        alert('>41');
                }

For some reason, the alert does not occur when the conditions are matched!

由于某种原因,当条件匹配时不会发生警报!

采纳答案by rochal

You should not use switchfor this scenario. This is the proper approach:

您不应该switch在这种情况下使用。这是正确的方法:

var cnt = $("#div1 p").length;

alert(cnt);

if (cnt >= 10 && cnt <= 20)
{
   alert('10');
}
else if (cnt >= 21 && cnt <= 30)
{
   alert('21');
}
else if (cnt >= 31 && cnt <= 40)
{
   alert('31');
}
else 
{
   alert('>41');
}

回答by deceze

A switch works by comparing what is in switch()to every case.

开关的工作原理是比较switch()每个case.

switch (cnt) {
    case 1: ....
    case 2: ....
    case 3: ....
}

works like:

像这样工作:

if (cnt == 1) ...
if (cnt == 2) ...
if (cnt == 3) ...

Therefore, you can't have any logic in the case statements.

因此,在 case 语句中不能有任何逻辑。

switch (cnt) {
    case (cnt >= 10 && cnt <= 20): ...
}

works like

if (cnt == (cnt >= 10 && cnt <= 20)) ...

and that's just nonsense. :)

那只是胡说八道。:)

Use if () { } else if () { } else { }instead.

使用if () { } else if () { } else { }来代替。

回答by Fabien Ménager

This should work with this :

这应该适用于:

var cnt = $("#div1 p").length;

            switch (true) {
                case (cnt >= 10 && cnt <= 20):
                    alert('10');
                    break;
                case (cnt >= 21 && cnt <= 30):
                   alert('21');
                    break;
                case (cnt >= 31 && cnt <= 40):
                    break;
                default:
                    alert('>41');
            }

回答by Pete Robie

Something I came upon while trying to work a spinner was to allow for flexibility within the script without the use of a ton of ifstatements.

我在尝试使用微调器时遇到的事情是允许脚本中的灵活性,而无需使用大量if语句。

Since this is a simpler solution than iterating through an array to check for a single instance of a class present it keeps the script cleaner. Any suggestions for cleaning the code further are welcome.

由于这是比遍历数组以检查存在的类的单个实例更简单的解决方案,因此可以使脚本更清晰。欢迎任何进一步清理代码的建议。

$('.next').click(function(){
        var imageToSlide = $('#imageSprite'); // Get id of image

        switch(true) {
            case (imageToSlide.hasClass('pos1')):
                imageToSlide.removeClass('pos1').addClass('pos2');
                break;
            case (imageToSlide.hasClass('pos2')):
                imageToSlide.removeClass('pos2').addClass('pos3');
                break;
            case (imageToSlide.hasClass('pos3')):
                imageToSlide.removeClass('pos3').addClass('pos4');
                break;
            case (imageToSlide.hasClass('pos4')):
                imageToSlide.removeClass('pos4').addClass('pos1');
        }
    }); ` 

回答by jAndy

What you are doing is to look for (0) or (1) results.

您正在做的是寻找 (0) 或 (1) 结果。

(cnt >= 10 && cnt <= 20) returns either true or false.

(cnt >= 10 && cnt <= 20) 返回真或假。

--edit-- you can't use case with boolean (logic) experessions. The statement cnt >= 10 returns zero for false or one for true. Hence, it will we case(1) or case(0) which will never match to the length.--edit--

--edit--您不能使用带有布尔(逻辑)表达式的 case。语句 cnt >= 10 为 false 返回 0,为 true 返回 1。因此,我们将永远不会与长度匹配的 case(1) 或 case(0) 。- 编辑 -

回答by Waqar Ahmed

function date_conversion(start_date){
    var formattedDate = new Date(start_date);
    var d = formattedDate.getDate();
    var m =  formattedDate.getMonth();
    var month;
    m += 1;  // JavaScript months are 0-11
    switch (m) {
        case 1: {
            month="Jan";
            break;
        }
        case 2: {
            month="Feb";
            break;
        }
        case 3: {
            month="Mar";
            break;
        }
        case 4: {
            month="Apr";
            break;
        }
        case 5: {
            month="May";
            break;
        }
        case 6: {
            month="Jun";
            break;
        }
        case 7: {
            month="Jul";
            break;
        }
        case 8: {
            month="Aug";
            break;
        }
        case 9: {
            month="Sep";
            break;
        }
        case 10: {
            month="Oct";
            break;
        }
        case 11: {
            month="Nov";
            break;
        }
        case 12: {
            month="Dec";
            break;
        }
    }
    var y = formattedDate.getFullYear();
    var now_date=d + "-" + month + "-" + y;
    return now_date;
}

回答by hariomthapa.in

Switch case is every help full instead of if else statement :

Switch case 是所有帮助,而不是 if else 语句:

     switch ($("[id*=btnSave]").val()) {
        case 'Search':
            saveFlight();
            break;
        case 'Update':
            break;
        case 'Delete':
            break;
        default:
            break;
    }