一个变量中的jQuery多个条件

Rea*_*per 7 javascript jquery

我试图在变量中添加条件然后在if()条件中赋值,但它没有按预期工作.

尝试过的可能性:

1)

conditionCheck = (getMonth == undefined || getMonth == "" || getMonth == null 
     || getDay == undefined || getDay == "" || getDay == null 
     || getYear == undefined || getYear == "" || getYear == null )
Run Code Online (Sandbox Code Playgroud)

2)

conditionCheck = getMonth == undefined || getMonth == "" || getMonth == null
     || getDay == undefined || getDay == "" || getDay == null 
     || getYear == undefined || getYear == "" || getYear == null 
Run Code Online (Sandbox Code Playgroud)

3)

conditionCheck = "getMonth == undefined || getMonth == "" || getMonth == null 
     || getDay == undefined || getDay == "" || getDay == null 
     || getYear == undefined || getYear == "" || getYear == null"
Run Code Online (Sandbox Code Playgroud)

但如果我在条件中添加,那么它的工作正常.

像这样. - >

if ( getMonth == undefined || getMonth == "" || getMonth == null 
       || getDay == undefined || getDay == "" || getDay == null 
       || getYear == undefined || getYear == "" || getYear == null ) {
} else {
  ageCalculation();
} 
Run Code Online (Sandbox Code Playgroud)

任何想法/建议?

编辑:

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

谢谢!!

vad*_*lim 5

TL; DR; 在JS,检查是否variable既不是null,undefined或者empty是简单地只是,

if (variable) {
  // 'variable' is not null, undefined or ''.
}
Run Code Online (Sandbox Code Playgroud)

你的第一第二个条件应该可以正常工作.

请注意,Date.getMonth()返回月份的从0开始的索引.如果它恰好发生在那个月 January,那么你的状况将不再有效getMonth,0 并且会让你conditionCheck变得永远true.

因为0 == "".

至于你的第三个条件,它是一个字符串,true在包装if条件时总会返回.

另一种方式,

所以,你想运行ageCalculation的功能,如果getMonth,getDay以及getYear不是 undefined,nullempty.

所以,而不是(您当前的代码)

if (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) {
   // ...
} else {
   ageCalculation();
}
Run Code Online (Sandbox Code Playgroud)

正如我上面状态下,检查是否变量或者是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 "".
}
Run Code Online (Sandbox Code Playgroud)


aki*_*ide 1

您可以使用 JS 三元运算符。https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

var getMonth;
var getDay;
var getYear;

var conditionCheck = (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) ? "Hello World" : "Hello Universe!"

console.log(conditionCheck) // Hello World
Run Code Online (Sandbox Code Playgroud)