简体   繁体   English

javascript如果条件&&条件不起作用

[英]javascript if condition && condition doesn't work

the AND && operator doesn't work, but when i replace it with an OR || AND &&运算符不起作用,但当我用OR ||替换它时 operation it is workin, why? 操作它是在工作,为什么? I just used OR || 我刚用OR || operator for testing, what i need is an && operator. 运算符用于测试,我需要的是一个&&运算符。 Please help. 请帮忙。 thanks 谢谢

function validate() {
    if ((document.form.option.value == 1) && (document.form.des.value == '')) {
        alert("Please complete the form!");
        return false
    } else return true;
}

i also tried nested if but it doesn't work too 我也试过嵌套,但它也不起作用

    if(document.form.option.value==1)
    {
         if(document.form.des.value=='')
        {   
            alert ("Please complete the form!");
            return false

        }
    }   

It sounds like || 听起来像是|| is what you are looking for here. 是你在这里寻找的。 The && operator is only true if both the left and right side of the && are true. &&如果左边和右边两个运营商是唯一真正&&是真实的。 In this case you appear to want to display the message if the value is 1 or empty. 在这种情况下,如果值为1或为空,您似乎希望显示消息。 This is exactly what the || 这正是|| operator is for. 运营商是为了。 It is true if either the left or right is true 如果左边或右边都是真的,则确实如此

If Or operator is working, means there are some javascript errors in your second part of condition. 如果Or运算符正在工作,则表示在第二部分条件中存在一些javascript错误。 check document.form.des.value=='' (maybe just open your javascript console in Chrome/Firefox/IE8+) 检查document.form.des.value ==''(也许只需在Chrome / Firefox / IE8 +中打开你的javascript控制台)

its because one of the conditions specified above returns false and loop breaks. 因为上面指定的条件之一返回false和循环中断。 Is you use OR ,only one must be validated and returns true.. check your code for both the conditions. 你使用OR,只有一个必须被验证并返回true ..检查你的代码是否有这两个条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM