一个变量中的jQuery多个条件

时间:2016-05-30 05:14:35

标签: javascript jquery

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

尝试过的可能性:

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

谢谢!

3 个答案:

答案 0 :(得分:5)

TL; DR; 在JS中,要检查variable是否nullundefinedempty仅仅是,< / p>

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

您的第一个第二个条件应该可以正常运作。

  

请注意,Date.getMonth()返回月份的基于0的索引。   如果它恰好发生在那个月   JanuarygetMonth0,您的情况将不再有效   并将conditionCheck变为true

     

因为0 == ""

至于第三条件(字符串),在true条件包装时始终会返回if

另一种方式,

因此,如果ageCalculationgetMonthgetDay getYearundefined {} {1}}或null

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

empty

如上所述,要检查变量是if (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) { // ... } else { ageCalculation(); } 还是undefined还是null (空),您可以将代码修改为,

""

答案 1 :(得分:1)

您可以使用JS Ternary Operator。 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

答案 2 :(得分:1)

if(myVariable){

//如果myVariable的值不是:    null或空字符串(“”)或未定义OR NaN OR false OR 0

}其他{

//如果myVariable的值等于    null或空字符串(“”)或未定义OR NaN OR false OR 0

}

因此,三元运算符可以如下:

var getMonth;

var getDay;

var getYear;

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

的console.log(conditionCheck); //如果getMonth,getDay,getYear的值不是null或空字符串(“”)或未定义OR NaN OR false OR 0

,则显示1