Javascript 如何在javascript中的if语句中指定多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8710442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:10:55  来源:igfitidea点击:

How to specify multiple conditions in an if statement in javascript

javascript

提问by adilahmed

Here is how I mention two conditions if this or this

这是我如何提到两个条件,如果这个或这个

if (Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')
    PageCount= document.getElementById('<%=hfPageCount.ClientID %>').value;
}

回答by AlanFoster

just add them within the main bracket of the if statement like

只需将它们添加到 if 语句的主括号中,例如

if ((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) {
            PageCount= document.getElementById('<%=hfPageCount.ClientID %>').value;
}

Logically this can be rewritten in a better way too! This has exactly the same meaning

从逻辑上讲,这也可以用更好的方式重写!这具有完全相同的含义

if (Type == 2 && (PageCount == 0 || PageCount == '')) {

回答by Edward Taylor

I am currently checking a large number of conditions, which becomes unwieldy using the if statement method beyond say 4 conditions. Just to share a clean looking alternative for future viewers... which scales nicely, I use:

我目前正在检查大量条件,使用超过 4 个条件的 if 语句方法变得笨拙。只是为了为未来的观众分享一个干净的替代品......它很好地扩展,我使用:

var a = 0;
var b = 0;

a += ("condition 1")? 1 : 0; b += 1;
a += ("condition 2")? 1 : 0; b += 1;
a += ("condition 3")? 1 : 0; b += 1;
a += ("condition 4")? 1 : 0; b += 1;
a += ("condition 5")? 1 : 0; b += 1;
a += ("condition 6")? 1 : 0; b += 1;
// etc etc

if(a == b) {
    //do stuff
}

回答by Glen Thompson

Here is an alternative way to do that.

这是一种替代方法。

const conditionsArray = [
    condition1, 
    condition2,
    condition3,
]

if (conditionsArray.indexOf(false) === -1) {
    "do somthing"
}

Or ES6

或者 ES6

if (!conditionsArray.includes(false)) {
   "do somthing"
}

回答by user40521

Sometimes you can find tricks to further combine statments.

有时您可以找到进一步组合语句的技巧。

Like for example:

例如:

0 + 0 = 0

and

"" + 0 = 0

so

所以

PageCount == 0
PageCount == ''

can be written like:

可以写成:

PageCount+0 == 0

In javascript 0is just as good as falseinverting !it would turn 0into true

在JavaScript0是一样好false反相!,将转0true

!PageCount+0

for a grand total of:

总计:

if ( Type == 2 && !PageCount+0 ) PageCount = elm.value;

回答by Fabrizio Calderan

the whole ifshould be enclosed in brackets and the oroperator is ||an not !!, so

整个if应该用括号括起来,or运算符是||not !!,所以

if ((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) { ...

回答by vaske

if((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) {

        PageCount= document.getElementById('<%=hfPageCount.ClientID %>').value;
}

This could be one of possible solutions, so 'or' is || not !!

这可能是可能的解决方案之一,所以 'or' 是 || 不是 !!

回答by Sergio Tulentsev

Wrap them in an extra pair of parens and you're good to go.

将它们包裹在一对额外的括号中,您就可以开始了。

if((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == ''))
    PageCount= document.getElementById('<%=hfPageCount.ClientID %>').value;
}

回答by Lilcent

function go(type, pageCount) {
    if ((type == 2 && pageCount == 0) || (type == 2 && pageCount == '')) {
        pageCount = document.getElementById('<%=hfPageCount.ClientID %>').value;
    }
}