代码之家  ›  专栏  ›  技术社区  ›  Chris Bier

javascript或if语句中

  •  0
  • Chris Bier  · 技术社区  · 15 年前

    我正试图在javascript中生成一个if语句,如果变量不等于几个不同的变量之一,它将起作用。我尝试过许多不同的或运算符变体,但我无法使其工作。

     if(var != "One" || "Two" || "Three"){
        // Do Something
        }
    

    有什么想法吗?谢谢!

    更新:

    我以前试过这个:

     if(var != "One" || var != "Two" || var != "Three"){
        // Do Something
        }
    

    因为某种原因,它不起作用。我的变量正在从DOM中提取信息,我不知道这是否会影响到它。

    实际代码

    // Gets Value of the Field (Drop Down box)
    var itemtype = document.forms[0].elements['itemtype' + i];
    
        if(itemtype.value != "Silverware" || itemtype.value != "Gold Coins" || itemtype.value != "Silver Coins"){   
    // Do Something
            }
    
    6 回复  |  直到 13 年前
        1
  •  8
  •   Nightfirecat peSHIr    13 年前

    你的表达总是正确的,你需要:

    if(!(myVar == "One" || myVar == "Two" || myVar == "Three")) { 
      // myVar is not One, Two or Three
    } 
    

    或:

    if ((myVar != "One") && (myVar != "Two") && (myVar != "Three")) {
      // myVar is not One, Two or Three
    }
    

    而且,对于短促:

    if (!/One|Two|Three/.test(myVar)) {
      // myVar is not One, Two or Three
    }
    // Or:
    if (!myVar.match("One|Two|Three")) {
      // ...
    }
    

    更多信息:

    编辑: 如果您选择最后一种方法,从您发布的代码开始 似乎 为了成为循环的一部分,我建议您在循环外部创建正则表达式,并使用 RegExp.prototype.test 方法而不是 String.prototype.match ,你也可能想关心 单词边界 也就是说,“没有人”会匹配“一个”没有他们…

        2
  •  6
  •   maerics    15 年前

    假设您的意思是“val不等于1或2或3”,那么De Morgan定理适用:

    if ((val != "One") && (val != "Two") && (val != "Three")) {
      // Do something...
    }
    
        3
  •  0
  •   Gattster    15 年前

    要获得更短的方法,请尝试此格式(复制自 http://snook.ca/archives/javascript/testing_for_a_v ):

    if(name in {'bobby':'', 'sue':'','smith':''}) { ... }
    

    function oc(a)
    {
      var o = {};
      for(var i=0;i<a.length;i++)
      {
        o[a[i]]='';
      }
      return o;
    }
    if( name in oc(['bobby', 'sue','smith']) ) { ... }
    
        4
  •  0
  •   Chinmay Kanchi    15 年前

    Mike提到的方法只适用于3个值,但如果您想将其扩展到 n 你的价值观 if 积木会很快变得丑陋。火狐1.5+和IE 8有一个 Array.indexOf 您可以这样使用的方法:

    if(["One","Two","Test"].indexOf(myVar)!=-1)
    {
        //do stuff
    }
    

    要在ie<=7上支持此方法,可以定义一个名为array.hasElement()的方法,如下所示:

    Array.prototype.hasElement = function hasElement(someElement)
    {
        for(var i=0;i<this.length;i++)
        {
            if(this[i]==someElement) 
                return true;
        }
        return false;
    }
    

    然后这样称呼它:

    if(!["One","Two","Three"].hasElement(myVar))
    {
        //do stuff
    }
    

    注意:只有在火狐中测试过,这是完全可行的。

        5
  •  -1
  •   I. J. Kennedy ShankarSangoli    15 年前

    除了将表达式扩展为三个子句之外,我认为您最好将变量命名为 var . 在JavaScript中, var 是关键字。大多数浏览器都不会提醒您该错误。

        6
  •  -1
  •   meder omuraliev    15 年前

    使用数组的其他方法:

    var selected = ['Silverware', 'Gold Coins', 'Silver Coins'];
    if ( selected.indexOf( el.value ) != -1 ) {
       // do something if it *was* found in the array of strings.
    }
    

    注: indexOf isnt a native method, grab the snippet here for IE: