简体   繁体   中英

Javascript multiple if true conditions

I neeed help with multiple if conditions.

if condition 1 & condition 2 true means I want both condition actions:

if(condition 1 true) {
    $('.inner_wrapper').addClass('em-border-red');
    return false;
}

if(condition 2 true) {
    $('.cCal').addClass('em-border-red');
    return false;
}

if(condition 3 true){
    $('.cCal-row2').addClass('em-border-red');
    return false;
}

But only one condition works.

Each of your if blocks contains a return statement. As soon as one condition is met, no further code will execute. This is, by definition, the behaviour of the return statement :

A return statement causes a function to cease execution and return a value to the caller

Since all of yours just return false , you should be able to move the return to after the conditions:

if(condition1) {
    $('.inner_wrapper').addClass('em-border-red');
}
if(condition2) {
    $('.cCal').addClass('em-border-red');
}
if(condition3) {
    $('.cCal-row2').addClass('em-border-red');
}
return false;

Remove the return false; form each if block and try to manage it at the bottom, outside of if condition.

retVal = true;
if(condition 1 true)
 {
     $('.inner_wrapper').addClass('em-border-red');
     retVal = false;

 }
if(condition 2 true)
 {
    $('.cCal').addClass('em-border-red');
     retVal = false;

 }
if(condition 3 true)
 {
    $('.cCal-row2').addClass('em-border-red');
     retVal = false;

 }
return retVal;

You have two ways:

    <!-- and condition //-->
    if (a == b && a != c) {
        // your stuff
    <!-- or condition //-->
    } else if (a == c || a ==b) {
        // other stuff
    <!-- otherwise //-->
    } else {
        // another stuff
    }

Otherwise you have "switch case". Like on this page .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM