javascript - 原始异常 : Cannot read property '0' of null

标签 javascript jquery

是的,我知道这是著名的可能重复问题之一。但我找不到如何在我的代码中解决它。

let newVlaue = [];
$(".displayInlineBlock").each(function() {
  if ($(this).find("input").is(':checked') && !$(this).find("label").hasClass("allselect")) {
    if ($(this).find("input").attr('id')) {
      let chosenValue = $(this).find("input").attr('id');
      let tempValue = chosenValue.match(/\d+/g);
      tempValue = parseInt(tempValue[0]);
      if (tempValue == 0) {
        newVlaue.push(tempValue);
      } else {
        newVlaue.push(tempValue - 1);
      }
    }
  }
});

感谢您的帮助

最佳答案

您需要检查 String#match 的结果是否是否为 null。如果没有,请继续。

let tempValue = chosenValue.match(/\d+/g); // result is null if no numbers are found
if (tempValue !== null) {
    // proceed
}

如果不匹配应包含零的值,您可以添加一个包含零的默认数组。

let tempValue = chosenValue.match(/\d+/g) || [0];

关于javascript - 原始异常 : Cannot read property '0' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48316412/

相关文章:

javascript - 在 javascript 中使用空 c# 模型

javascript - 如何将表格(固定标题)中每个标题单元格的宽度设置为与其列中最大的宽度相同,并使浏览器尊重它?

jquery - 需要正确的jquery选择器

javascript - 如何根据输入 id 创建名称为对象的属性?

javascript弹窗文件永远不会是 "ready"

javascript - 如何处理许多异步回调的完成

javascript - Angular Input Upload - 获取文件名并上传

jquery - 使用 JQuery().text() 返回 int 和 string

javascript - 使用 javascript 的 html5 下拉列表

php - 如何使用php获取大量数据并将其加载到jquery数据表中