本文介绍了JavaScript中的Switch中单个情况下的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中使用Switch语句,我需要在多个情况下应用单个情况

我将代码编写为:

I am using Switch statement in JavaScript,I need to apply single case on multiple cases

I am writing code as:

function SelectDropDownValue(Service_Name, DefaultValue, Fieldname) {
    var result;
    switch (Service_Name) {
        case ("SUBDIVIDED" || "ARB" || "MINING CLAIM" || "SECTIONAL" || "MAPS AND SUBDIVISIONS"):
            if (DefaultValue == "N" && Fieldname == "BACKTOBASEORDEROPTION") {
                result = "PLANT_BEGINNING";
                break;
            }
            else if (DefaultValue == "1") {
                result = "MOST_RECENT";
                break;
            }
            else if (DefaultValue == "2") {
                result = "SECOND_MOST";
                break;
            }
            else if(DefaultValue == "N" && Fieldname=="SORTINVESTIGATIVEOPTION" || Fieldname=="SORTORDEROPTION")
            {
            result ="Newest";
            break;
            }//End       
        default: result = ""; break;
    } //end switch case
    return result;
}



出现问题是该案例仅针对细分,而不针对其他值运行



Problem is arising is that the case is running only for Subdivided and not for other values

How can i do this in JS?

推荐答案

<pre lang="cs">function SelectDropDownValue(Service_Name, DefaultValue, Fieldname) {<br />
    var result;<br />
    switch (Service_Name) {<br />
        case ("SUBDIVIDED"): <br />
        case ("ARB"): <br />
        case ("MINING CLAIM"): <br />
        case ("SECTIONAL"): <br />
        case ("MAPS AND SUBDIVISIONS"):<br />
            if (DefaultValue == "N" && Fieldname == "BACKTOBASEORDEROPTION") {<br />
                result = "PLANT_BEGINNING";<br />
                break;<br />
            }<br />
            else if (DefaultValue == "1") {<br />
                result = "MOST_RECENT";<br />
                break;<br />
            }<br />
            else if (DefaultValue == "2") {<br />
                result = "SECOND_MOST";<br />
                break;<br />
            }<br />
            else if(DefaultValue == "N" && Fieldname=="SORTINVESTIGATIVEOPTION" || Fieldname=="SORTORDEROPTION")<br />
            {<br />
            result ="Newest";<br />
            break;<br />
            }//End<br />
        default: result = ""; break;<br />
    } //end switch case<br />
    return result;<br />
}</pre><br />



JavaScript对switch语句使用了类似于C的语法,如上所示,因此仅使用换行符和另一种情况就可以正常工作.



JavaScript uses C like syntax for switch statement as shown above and so just using a newline and another case should work fine.



function SelectDropDownValue(Service_Name, DefaultValue, Fieldname) {
    var result;
    switch (Service_Name) {
        case "SUBDIVIDED":
        case "ARB":
        case "MINING CLAIM":
        case "SECTIONAL":
        case "MAPS AND SUBDIVISIONS":
            if (DefaultValue == "N" && Fieldname == "BACKTOBASEORDEROPTION") {
                result = "PLANT_BEGINNING";
            }
            else if (DefaultValue == "1") {
                result = "MOST_RECENT";
            }
            else if (DefaultValue == "2") {
                result = "SECOND_MOST";
            }
            else if(DefaultValue == "N" && Fieldname=="SORTINVESTIGATIVEOPTION" || Fieldname=="SORTORDEROPTION")
            {
                result ="Newest";
            }//End
            break;
        default:
            result = "";
            break;
    } //end switch case
    return result;
}



这样,它应该可以工作.顺便说一句,我把所有的休息都从else-ifs移开了,因为我更喜欢这种风格,在这种情况下,确实没有区别.

祝您编码愉快!



That way it should work. BTW I moved all of your breaks out from the else-ifs because I like that style better and in this case there really isn''t a difference.

Happy coding!


这篇关于JavaScript中的Switch中单个情况下的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 09:18