修改JS代码:实现已选下拉框间无跳过的告警功能
修改后的JavaScript代码及逻辑说明
修正后的完整代码
if (document.getElementById('actionId').value == 'recommendWithFin') { var curRevSeqId = parseInt(document.getElementById('currentReviewerSequence').value); var allRevLen = allReviewersArray.length; if (allRevLen <= curRevSeqId) { alert('You must select the next approver before recommending the quote.'); valid = valid && false; } else { // 收集当前审核序列之后所有已选中的审核者索引 var selectedIndices = []; for (var i = curRevSeqId; i < allRevLen; i++) { if (allReviewersArray[i] != null && allReviewersArray[i] !== '') { selectedIndices.push(i); } } // 仅当选中至少两位审核者时,检查是否存在跳过 if (selectedIndices.length >= 2) { var firstSelected = selectedIndices[0]; var lastSelected = selectedIndices[selectedIndices.length - 1]; // 遍历第一个到最后一个选中索引的区间,检查是否有空项 for (var j = firstSelected; j <= lastSelected; j++) { if (allReviewersArray[j] == null || allReviewersArray[j] === '') { alert('Please do not skip approver'); valid = valid && false; break; } } } } }
关键逻辑调整说明
- 修复索引越界问题:原代码遍历
i <= allRevLen会访问数组外的undefined项,改为i < allRevLen匹配数组索引规则。 - 精准筛选选中项:先收集当前审核序列之后所有已选中的审核者索引,避免无差别遍历全部项导致的误触发。
- 区间检查跳过行为:仅当选中至少两位审核者时,检查第一个到最后一个选中项的区间内是否存在空项——只要区间内有空项,说明存在跳过;若仅选中单个项或未选中全部项但区间内无空项,不会触发告警。
- 类型安全处理:将
curRevSeqId转为整数,避免字符串与数字比较的潜在错误。
内容的提问来源于stack exchange,提问作者divya srinivas
相关产品推荐
相关产品推荐

