Javascript jQuery if语句中的多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10722682/
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 02:39:52  来源:igfitidea点击:

jQuery multiple conditions within if statement

javascriptjquery

提问by MG1

What is the syntax for this loop to skip over certain keys? The way I have it written is not working properly.

这个循环跳过某些键的语法是什么?我写的方式不能正常工作。

 $.each(element, function(i, element_detail){
    if (!(i == 'InvKey' && i == 'PostDate')) {
        var detail = element_detail + ' ';
        $('#showdata').append('<div class="field">' + i + detail + '</div>');
       }
 });

回答by CD Smith

Try

尝试

if (!(i == 'InvKey' || i == 'PostDate')) {

or

或者

if (i != 'InvKey' || i != 'PostDate') {

that says if i does not equals InvKeyOR PostDate

表示如果 i 不等于InvKeyORPostDate

回答by SLaks

i == 'InvKey' && i == 'PostDate'will never be true, since ican never equal two different things at once.

i == 'InvKey' && i == 'PostDate'永远不会是真的,因为i永远不可能同时等于两个不同的东西。

You're probably trying to write

你可能正在尝试写

if (i !== 'InvKey' && i !== 'PostDate')) 

回答by WTellos

A more general approach:

更通用的方法:

if ( ($("body").hasClass("homepage") || $("body").hasClass("contact")) && (theLanguage == 'en-gb') )   {

       // Do something

}