简体   繁体   English

JavaScript if else 条件混乱

[英]JavaScript if else condition confusion

I have below code我有以下代码

if (gridObj.INVOICEORDERNUMBER) {
  if (!cancelledStatus.length && !withDrawn.length) {
    this.gridCmp.editValidation = true;
    this.errorMessage = ErrorMessage.OpenOrderMsg;
  } else {
    this.relatedLicenses = false;
    this.hasDRLPLicense = false;
    this.deletePopupMsg = ErrorMessage.DeletePopupMsg;
    this.showDeletePopup = true;
  }
} else if (this.hasDRLPLicense) {
  this.gridCmp.editValidation = true;
  this.errorMessage2 = ErrorMessage.DRLPLicenseDeleteMsg;
} else {
  this.relatedLicenses = false;
  this.hasDRLPLicense = false;
  this.deletePopupMsg = ErrorMessage.DeletePopupMsg;
  this.showDeletePopup = true;
}

Here I need to check whether both if conditions satisfy or not.在这里,我需要检查两个条件if满足。 With the current approach, I can only check anyone if condition, as it is an if-else , if statement.使用当前的方法,我只能检查任何人的 if 条件,因为它是if-else , if语句。

What I need to achieve here is我需要在这里实现的是

  1. If first if condition is satisfied -> show errorMessage如果首先if条件 -> show errorMessage
  2. If second if condition (else if) satisfied -> show errorMessage2如果第二个if条件(else if)满足 -> show errorMessage2
  3. If both satisfied -> show both errorMessage and errorMessage2如果都满足 -> show both errorMessage and errorMessage2
  4. If none satisfied -> execute else如果没有满足 -> execute else

This might be a stupid question, but my brain is not working any more today.这可能是一个愚蠢的问题,但我的大脑今天不再工作了。 Please help.请帮忙。 Thanks谢谢

According to the logic you explained, this is the truth table you are looking for:根据您解释的逻辑,这是您要查找的真值表:

cond1 | cond2 | errorMessage | errorMessage2 | execute 
------+-------+--------------+---------------+--------
false | false |  NO          |  NO           |  YES
false | true  |  NO          |  YES          |  NO
true  | false |  YES         |  NO           |  NO
true  | true  |  YES         |  YES          |  NO

This is the code which will accomplish it:这是将完成它的代码:

if (cond1 || cond2) {
  if (cond1) { 
    // show errorMessage
  }

  if (cond2) { 
    // show errorMessage2
  }
} else {
  // execute ...
}

Try this code.试试这个代码。

if (gridObj.INVOICEORDERNUMBER || this.hasDRLPLicense) {
  this.gridCmp.editValidation = true;

  if (gridObj.INVOICEORDERNUMBER && !cancelledStatus.length && !withDrawn.length) {  
    this.errorMessage = ErrorMessage.OpenOrderMsg;
  }
  if (this.hasDRLPLicense) {
    this.errorMessage2 = ErrorMessage.DRLPLicenseDeleteMsg;
  }
} else {
    this.relatedLicenses = false;
    this.hasDRLPLicense = false;
    this.deletePopupMsg = ErrorMessage.DeletePopupMsg;
    this.showDeletePopup = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM