You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript条件判断逻辑调整:实现多条件独立校验与多错误信息展示

解决if-else分支无法同时校验多条件的问题

我明白你的痛点——原来的嵌套if-else是互斥逻辑,只能触发其中一个分支,没法同时校验并展示两个条件的错误提示。咱们可以把两个判断条件拆成独立的检查,这样就能完美覆盖你需要的四种场景了,具体修改方案如下:

修改后的代码

// 先重置状态,避免之前的残留值影响判断
this.gridCmp.editValidation = false;
this.errorMessage = '';
this.errorMessage2 = '';

// 单独判断第一个条件:存在发票订单号且无取消/撤回状态
const meetsFirstCondition = gridObj.INVOICEORDERNUMBER && !cancelledStatus.length && !withDrawn.length;
if (meetsFirstCondition) {
  this.gridCmp.editValidation = true;
  this.errorMessage = ErrorMessage.OpenOrderMsg;
}

// 单独判断第二个条件:拥有DRLP许可证
if (this.hasDRLPLicense) {
  this.gridCmp.editValidation = true;
  this.errorMessage2 = ErrorMessage.DRLPLicenseDeleteMsg;
}

// 当两个条件都不满足时,执行删除弹窗逻辑
if (!meetsFirstCondition && !this.hasDRLPLicense) {
  this.relatedLicenses = false;
  this.hasDRLPLicense = false;
  this.deletePopupMsg = ErrorMessage.DeletePopupMsg;
  this.showDeletePopup = true;
}

逻辑说明

这样拆分后,四个场景都能被正确覆盖:

  • 仅满足第一个条件:只会设置errorMessage,editValidation被设为true
  • 仅满足第二个条件:只会设置errorMessage2,editValidation被设为true
  • 同时满足两个条件:errorMessage和errorMessage2都会被赋值,editValidation同样为true(两次赋值不冲突)
  • 两个条件都不满足:触发删除弹窗的相关逻辑

内容的提问来源于stack exchange,提问作者vamsi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 09:59:08