Using OR operator in a jquery if statement

后端 未结 4 1305
悲&欢浪女
悲&欢浪女 2021-01-03 00:58

I need to use the OR operator in a jQuery if statement to filter out 10 states. My code works wile only excluding one state, but fails when i try to include multiple states.

4条回答
  •  悲哀的现实
    2021-01-03 01:04

    The code you wrote will always return true because state cannot be both 10 and 15 for the statement to be false. if ((state != 10) && (state != 15).... AND is what you need not OR.

    Use $.inArray instead. This returns the index of the element in the array.

    JSFIDDLE DEMO

    var statesArray = [10, 15, 19]; // list out all
    
    var index = $.inArray(state, statesArray);
    
    if(index == -1) {
        console.log("Not there in array");
        return true;
    
    } else {
        console.log("Found it");
        return false;
    }
    

提交回复
热议问题