Jak sprawdzić, czy zmienna nie jest równa żadnej z dwóch wartości?

Chcę napisać instrukcję if / else, która sprawdza, czy wartość wejściowego tekstu nie jest równa jednej z dwóch różnych wartości. Tak (przepraszam mój pseudo-angielski kod):

var test = $("#test").val();
if (test does not equal A or B){
    do stuff;
}
else {
    do other stuff;
}

Jak napisać warunek dla instrukcji if w linii 2?

Author: daGUY, 2011-05-24

7 answers

Pomyśl o ! (operator negacji) jako "nie", || (Operator boolean-lub) jako "or" I && (Operator boolean-i) jako "and". Zobacz operatory i pierwszeństwo operatora.

Tak więc:

if(!(a || b)) {
  // means neither a nor b
}

Jednak, używając prawa De Morgana , można go zapisać jako:

if(!a && !b) {
  // is not a and is not b
}

a i b powyżej może być dowolne wyrażenie (np. test == 'B' lub cokolwiek to musi być).

Jeszcze raz, jeśli test == 'A' i test == 'B' są wyrażeniami, zwróć uwagę na rozszerzenie pierwsza forma:

// if(!(a || b)) 
if(!((test == 'A') || (test == 'B')))
// or more simply, removing the inner parenthesis as
// || and && have a lower precedence than comparison and negation operators
if(!(test == 'A' || test == 'B'))
// and using DeMorgan's, we can turn this into
// this is the same as substituting into if(!a && !b)
if(!(test == 'A') && !(test == 'B'))
// and this can be simplified as !(x == y) is the same as (x != y)
if(test != 'A' && test != 'B')
 99
Author: Michał Perłakowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-06 20:51:37

Ecma2016 Najkrótsza odpowiedź, szczególnie dobra przy sprawdzaniu wielu wartości:

if (!["A","B", ...].includes(test)) {}
 19
Author: CESCO,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-29 23:21:34

Ogólnie byłoby to coś takiego:

if(test != "A" && test != "B")

Powinieneś poczytać o operatorach logicznych JavaScript.

 8
Author: James Montagne,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-16 14:56:34

Robię to używając jQuery

if ( 0 > $.inArray( test, [a,b] ) ) { ... }
 2
Author: Zlatev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-28 22:10:50
var test = $("#test").val();
if (test != 'A' && test != 'B'){
    do stuff;
}
else {
    do other stuff;
}
 1
Author: AlbertVo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-24 19:38:32

Użyłeś słowa " lub " w swoim pseudo kodzie, ale bazując na pierwszym zdaniu, myślę, że masz na myśli i. Było pewne zamieszanie, ponieważ ludzie zwykle tak nie mówią.

Chcesz:

var test = $("#test").val();
if (test !== 'A' && test !== 'B'){
    do stuff;
}
else {
    do other stuff;
}
 1
Author: sophistihip,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-13 10:27:13

Czy Mogę zasugerować użycie instrukcji in else if w instrukcji if / else. A jeśli nie chcesz uruchamiać żadnego kodu, który nie pod żadnym warunkiem, możesz po prostu zostawić else na końcu instrukcji. else if może być również używany dla dowolnej liczby ścieżek dywersji, które wymagają, aby dla każdej z nich był określony warunek.

If(warunek 1){

} else if (warunek 2) {

}else {

}

 0
Author: JohnnyBoyy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-15 13:54:27