Multiple comparisons in an if statement using the logical OR operator

前端 未结 5 1875
长发绾君心
长发绾君心 2020-12-11 10:33

I want to do he following, but it does not work:

if(pathname == \'/ik/services/\' || \'/ik/recruitment/\'){
   //run function
}

It is compl

相关标签:
5条回答
  • 2020-12-11 11:03

    try doing

        if(pathname == '/ik/services/' || pathname == '/ik/recruitment/'){
          //run function
        }
    
    0 讨论(0)
  • 2020-12-11 11:10

    Try this:

    if((pathname == '/ik/services/') || (pathname == '/ik/recruitment/')){
       //run function
    }
    
    0 讨论(0)
  • 2020-12-11 11:15

    You would have to do something like this:

    if(pathname == '/ik/services/' || pathname == '/ik/recruitment/'){
       //run function
    }
    

    Your || '/ik/recruitment/' would always be truthy, and therfor the code within your if-statement will always run.

    0 讨论(0)
  • 2020-12-11 11:15

    This has nothing to do with jQuery, it's just a normal JS "error". You need to compare both strings with the variable:

    if (pathname == 'foo'  || pathname == 'bar') 
    
    0 讨论(0)
  • 2020-12-11 11:19

    Try

    if(pathname == '/ik/services/' || pathname == '/ik/recruitment/'){
    

    OR

    jQuery inArray

    Example

    var arr = ['/ik/services/','/ik/recruitment/'];
    
    if($.inArray(pathname , arr) !== -1)
    
    0 讨论(0)
提交回复
热议问题