2016-05-30 4 views
7

条件に変数を追加しようとしていて、if()条件で代入しようとしましたが、期待通りに機能しません。jQuery複数の条件を1つの変数

しようとした可能性:

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" 

しかし、私は、その後の状態でその作業であるとして追加した場合良い。

このようです。 - >

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

任意のアイデア/提案ですか?

編集:

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(); 
    } 
} 

感謝!!

+1

didnotの仕事はどういう意味ですか? conditionCheck変数 –

+0

であなたは何をしましたか? また、3番目の文字列は 'true'と評価される文字列です。 –

+0

データ型に応じてvariablesadd stringまたはnumを追加すると良いです。変数は条件のチェックには使用できません。ただし、変数の前にletを追加したい場合は – mayur

答えて

5

TL; DR。 JSではvariableはどちらもnullundefinedemptyであるかどうかを確認することがちょうどによって単純で、

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

あなた最初条件はうまく動作するはずです。

注意してください、Date.getMonthは()月の0から始まるインデックスを返します。 そして、それはちょうど月が Januaryであることを起こればgetMonth0 であり、常にtrueであるためにあなたのconditionCheckを向けるだろうから、あなたの条件が機能しなくなります。

0 == ""です。文字列であるあなたの第三条件については

if条件に包まれたときに、常にtrueを返します。

別の方法、だから、

、あなたは、getMonth場合ageCalculation機能を実行したいgetDay、およびgetYearないundefinednullまたはemptyです。代わりにのだから、

、(あなたの現在のコード)

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

私は上記の状態として、変数がundefinedまたはnullあるいは""(空)のいずれかであるかどうかを確認するために、あなたのコードを変更することができます、へ

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

あなたは_ "WHY" _に答えると思われますか?なぜあなたが '&'の上に '|| ' – Rayon

+0

@レイヨン私は彼の「第3の」条件を得ていません。だから私は今、私の提案を与えてくれるでしょう.. – choz

+1

@Rayon私は '&&' over '||'を好むわけではありませんが、それは逆のロジックがどのように動作するかです。 – choz

1

(myVariable変数)であれば{

// myVariable変数の値がない場合: ヌルまたは空の文字列(「」)またはundefinedまたはNaNまたはFALSE OR 0

}他{

// myVariable変数の値が に等しい場合nullまたは空の文字列(「」)OR未定義またはNaNまたはFALSE OR 0

}

だから、三項演算子はようになります

var getMonth;

var getDay;

var getYear;

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

console.log(conditionCheck); // getMonth、getDay、getYearのいずれかがNULL以外の値を持つ場合は1を表示するOR空の文字列( "")OR未定義OR NaN OR偽OR 0

関連する問題