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

如何查找表格中下一个带非空文本值的volume类元素并计算日消耗量

表格消耗量计算逻辑修复(空值兼容版)

修复后代码

$('.volume').each(function() {
  // 校验当前volume值有效性
  const currentText = $(this).text().trim()
  const currentNum = Number(currentText)
  if (!currentText || isNaN(currentNum)) {
    $(this).siblings('.consumed').text('')
    return
  }

  // 遍历后续所有行,查找第一个有效volume值
  let nextValidNum = null
  $(this).parent().nextAll('tr').each(function() {
    const nextText = $(this).find('.volume').text().trim()
    const nextNum = Number(nextText)
    if (nextText && !isNaN(nextNum)) {
      nextValidNum = nextNum
      // 找到目标后终止遍历
      return false
    }
  })

  // 计算结果或留空
  if (nextValidNum !== null) {
    $(this).siblings('.consumed').text(nextValidNum - currentNum)
  } else {
    $(this).siblings('.consumed').text('')
  }
})

逻辑说明

  • 先校验当前行volume值是否为有效数字,空值、非数字直接跳过,对应消耗量列留空
  • 从当前行的下一行开始遍历所有后续表格行,跳过所有volume为空的行,直到找到第一个有有效数值的volume元素
  • 找到有效数值后做差值计算填入消耗量列,遍历到表格末尾都没有找到有效值则消耗量列留空

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 07:57:03