Javascript多个OR语句无法正常工作

时间:2015-12-09 10:15:43

标签: javascript jquery

我在Javascript(jQuery)中有这个简单的代码,我需要访问<input type="button" class="button" onclick="sendtoajax(this.form)" /> 语句,以防一个条件成为IF,但我总是获得TRUE,即使条件也是如此已经TRUE,我不明白为什么。这是简单的代码:

FALSE

我想要的只是for(var i = 0; i < vector.length; i++) { genere = vector[i].gender; pitch = synthProcess[i].pitch; mood = synthProcess[i].mood; speed = synthProcess[i].speed; identifier = synthProcess[i].ident; if (genere !== "auto" || pitch !== "0" || mood !== "0" || speed !== "0" || identifier !== "0"){ console.log("EXECUTING COMMAND"); } } 与'auto'不同,genere与'0'等不同,然后进入pitch但始终进入。 我尝试使用“0”(字符串)和0(int)选项,但仍然得到相同的结果,在每次迭代中我得到IF

内的Log

2 个答案:

答案 0 :(得分:1)

您正在进行严格的比较,它也会检查变量的类型。像这样:

var a = 'auto';
var b = 0;
var c = 0;

if (a !== 'auto' || b !== '0' || c !== '0') {
    // this will be called since (0 !== '0') = true
    console.log('called!');
}

if (a !== 'auto' || b != '0' || c != '0') {
    // this wont be called since (0 != '0') = false
    console.log('not called');
}

也就是说,如果使用严格比较将零int与0 String进行比较,它们将被视为不同:

console.log(0 === '0') // prints false
console.log(0 !== '0') // prints true

如果您使用正常比较进行比较,它们将被视为相同:

console.log(0 == '0') // prints true
console.log(0 != '0') // prints false

结帐this explanationmy fiddle

答案 1 :(得分:1)

如果音高等的值是int 使用 -

var  genere = 'auto';
var pitch = 0;
var   mood = 0;
var speed = 0;
var  identifier = 0;
if (genere !== "auto" || pitch !== 0 || mood !== 0 || speed !== 0 || identifier !== 0){
        alert("EXECUTING COMMAND");
}

否则,音高等值是字符串[ex:&#39; 0&#39;]然后使用

var  genere = 'auto';
var pitch = "0";
var   mood = "0";
var speed = "0";
var  identifier = "0";
if (genere !== "auto" || parseint(pitch) !== 0 || parseint(mood) !== 0 || parseint(speed) !== 0 || parseint(identifier) !== 0){
        alert("EXECUTING COMMAND");
}