2016-05-30 18 views
7

Próbuję dodać warunki do zmiennej, a następnie przypisać w warunku if(), ale nie działa zgodnie z oczekiwaniami.jQuery Wiele warunków w jednej zmiennej

Wypróbowane możliwości:

1)

conditionCheck = (getMonth == undefined || getMonth == "" || getMonth == null 
    || getDay == undefined || getDay == "" || getDay == null 
    || getYear == undefined || getYear == "" || getYear == null) 

2)

conditionCheck = getMonth == undefined || getMonth == "" || getMonth == null 
    || getDay == undefined || getDay == "" || getDay == null 
    || getYear == undefined || getYear == "" || getYear == null 

3)

conditionCheck = "getMonth == undefined || getMonth == "" || getMonth == null 
    || getDay == undefined || getDay == "" || getDay == null 
    || getYear == undefined || getYear == "" || getYear == null" 

ale jeśli dodać jak jest w stanie następnie jego pracy w porządku.

lubię to. ->

if (getMonth == undefined || getMonth == "" || getMonth == null 
     || getDay == undefined || getDay == "" || getDay == null 
     || getYear == undefined || getYear == "" || getYear == null) { 
} else { 
    ageCalculation(); 
} 

Każdy pomysł/sugestia?

Zmieniano:

function ageCalculation() { 
    getDate = getYear + "-" + getMonth + "-" + getDay; 
    dob = new Date(getDate); 
    today = new Date(); 
    age = (today - dob)/(365.25 * 24 * 60 * 60 * 1000); 
    if (age < 3 || age == 3 || age > 3 && age < 3.00452422294471007) { 
    $('.greater-msg, .less-then-msg').remove(); 
    $(contentParent).find('.fieldset-wrapper').after('<div class="less-then-msg">Disclaimer: In compliance with EO51, cannot directly engage with mothers with children aged 0 to 3 years old. All content that you will receive via email will only be regarding your pregnancy. </div>'); 
    } else if (age > 3) { 
    $('.greater-msg, .less-then-msg').remove(); 
    $(contentParent).find('.fieldset-wrapper').after('<div class="greater-msg">You can also visit to know how you can keep giving your child the 360 advantage.</div>'); 
    } 
    if (age <= -1 || age <= -0 || age == 0 || age == -0) { 
    $('.greater-msg, .less-then-msg').remove(); 
    } 
} 

Dzięki !!

+1

Co masz na myśli, mówiąc, że nie działa. co zrobiłeś ze zmienną conditionCheck –

+0

nie masz ciała dla swojego 'if '. Również trzecia to tylko ciąg, który oceni "prawda". –

+0

to dobrze, aby dodać ciąg znaków zmienna lub liczba zgodnie z typem danych. Zmienne nie są dla Sprawdź warunki .. chociaż jeśli chcesz dodać let przed zmiennymi – mayur

Odpowiedz

5

TL; DR; W JS, aby sprawdzić, czy nie jest ani nullvariable, undefined lub empty jest po prostu po prostu,

if (variable) { 
    // 'variable' is not null, undefined or ''. 
} 

Twój pierwszy i sekund warunek powinien działać dobrze.

Należy pamiętać, Date.getMonth() zwraca indeks 0 opartego miesiąca. A jeśli po prostu zdarza się, że miesiąc ma January, twój stan nie będzie już pracować od getMonth jest 0 i zmieni swoją conditionCheck być zawsze true.

Ponieważ 0 == "".

Jak dla trzecim stanu, który jest ciągiem znaków, zawsze zwróci true gdy owinięty w if stanie.

Alternatywnym sposobem,

Więc chcesz uruchomić ageCalculation funkcję jeśli getMonth, getDay i getYearnieundefined, null lub empty.

Więc zamiast (Twój aktualny kod)

if (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) { 
    // ... 
} else { 
    ageCalculation(); 
} 

Jak stwierdzić powyżej, do sprawdzenia, czy zmienna jest albo undefined lub null lub nawet ""(pusty), można zmodyfikować kod do,

if (getMonth && getDay && getYear) { 
    // getMonth, getDay, and getYear are not null, undefined or "". 
    ageCalculation(); 
} 
else { 
    // Either getMonth, getDay and getYear have a value of null, undefined or "". 
} 
+1

Masz odpowiedzieć _ "DLACZEGO" _? Wspomnij też, dlaczego wolałeś '&&' over' || ' – Rayon

+0

@Rayon Nie mam dość warunku" 3rd ". Więc na razie udzielę mojej sugestii .. – choz

+1

@Rayon To nie jest tak, że wolę '&&' over '||', ale tak działa odwrotna logika. – choz

1

if (myVariable) {

// Jeśli wartość myVariable nie jest: null lub pusty ciąg („”) lub nieokreślone lub NaN czy fałsz lub 0

} else {

// Jeśli wartość myVariable jest równa null lub pusty ciąg („”) lub undefined NaN OR OR fałszywe lub 0

}

Więc Ternary Operator może być tak:

var getMonth;

var getDay;

var getYear;

var conditionCheck = (getMonth || getDay || getYear)? "1": "0";

console.log (conditionCheck); // wyświetla 1, jeśli albo getMonth, getDay, getYear mają wartości inne niż NULL LUB pusty ciąg znaków ("") LUB niezdefiniowany OR NaN LUB fałsz LUB 0