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

jQuery多字段值相等判断异常:空字段仍显示False求助

问题分析与修复

核心问题

  1. 逻辑判断错误:原代码的条件overduer && overdue == fieldNameForResult仅判断了additional_personnel_costs非空且overdue等于目标值,完全不符合“两个字段都等于current_outstanding_balance”的需求。
  2. 空值处理缺陷:当所有字段为空时,overduer(即additional_personnel_costs)为空,条件直接不成立,导致返回False,但空值与空值应判定为相等。
  3. 变量名不规范:overduer是拼写失误,建议改为更清晰的命名(比如additionalCosts),提升代码可读性。

修复后的代码

function(){
   var targetBalance = thisPointer.entity.getValue('current_outstanding_balance');
   var additionalCosts = thisPointer.entity.getValue('additional_personnel_costs');
   var overdue = thisPointer.entity.getValue('overdue');

   // 判断两个字段的值都与目标值相等(包括空值相等的情况)
   if(additionalCosts === targetBalance && overdue === targetBalance){
       jQ('[id="current_outstanding_balance_check"]').val('True');
   }else{
       jQ('[id="current_outstanding_balance_check"]').val('False');
   }
}

关键说明

  • 使用严格相等运算符===:避免隐式类型转换导致的判断错误(比如字符串"0"和数字0被误判为相等)。
  • 修正判断逻辑:确保additional_personnel_costs和overdue同时等于current_outstanding_balance才返回True。
  • 空值场景适配:当三个字段都为空时,additionalCosts === targetBalance和overdue === targetBalance都会成立,正确返回True。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 11:10:49