javascript 结合两个if语句的逻辑

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

Combining the logic of two if statements

javascript

提问by Aumkar Thakur

Is there a way to make the following JavaScript if..else condition short, instead of writing if() else()function two times is there any way to write if(x,y = ""){} else{}or something similar to make it shorter ?

有没有办法让下面的 JavaScript if..else 条件变短,而不是写if() else()两次函数,有没有办法写if(x,y = ""){} else{}或类似的东西让它更短?

$(document).ready(function(){
    $("#third").click(function(){
        var xxx = $("#first").val();
        var xxy = $("#second").val();

        if (xxx == "") {
            alert("good"); 
        }
        else {
            alert("bad"); 
        }

        if (xxy == "") {
            alert("good"); 
        }
        else {
            alert("bad"); 
        }
    });
});

回答by Mr. Alien

No, you cannot write a condition like that, instead you can write it as

不,你不能写这样的条件,相反,你可以把它写成

if(xxx == '' || yyy == '') {}

Or

或者

if(xxx == '' && yyy == '') {}

Here, ||is an OR operator and &&is AND operator, the difference is 1st will return true if 1 condition satisfies, 2nd one will return true if both condition satisfies.

这里,||是 OR 运算符和&&是 AND 运算符,区别在于如果满足 1 个条件,则第一个将返回真,如果两个条件都满足,则第二个将返回真。

If you are looking for a short hand syntax, you can use ternary operator

如果您正在寻找简写语法,您可以使用三元运算符

回答by SmokeyPHP

Use the "or" syntax (||)

使用“或”语法 ( ||)

if(xxx == '' || yyy == '')
{
    alert("good");
}
else
{
    alert("bad");
}

This way, if xxxis an empty string OR yyyis an empty string, it will alert "good". If neither of them are empty, it will alert "bad"

这样,如果xxx是空字符串或者yyy是空字符串,它会提示“好”。如果它们都不为空,则会提示“坏”

回答by Maxim Zhukov

What about this?

那这个呢?

$(document).ready(function() {
    var showResult = function(val) {
        alert(val ? "good" : "bad");
    };

    $("#third").click(function(){

        var xxx = $("#first").val();
        var xxy = $("#second").val();

        showResult(xxx);
        showResult(xxy);
    });
});

If you want just to combine, you can write even less:

如果你只想组合,你可以写得更少:

$(document).ready(function() {
    $("#third").click(function(){
        var xxx = $("#first").val();
        var xxy = $("#second").val();

        alert((xxx || xxy) ? "good" : "bad");
    });
});

and if you want the shortes code, i think it could be like this:

如果你想要shortes代码,我认为它可能是这样的:

$(document).ready(function() {
    $("#third").click(function(){
        var elems = $("#first, #second").val();

        alert((elems[0] || elems[1]) ? "good" : "bad");
    });
});

Demo

演示

回答by Anton

You can write it like this

你可以这样写

xxx=="" && xxy =="" ? alert("good") : alert("bad")

回答by Farhad

    if(xxx=="" && xxy == ""){
alert('good');
}else if(xxx!="" || xxy != ""){
alert('bad');}