是的,我知道这是著名的可能重复问题之一。但我找不到如何在我的代码中解决它。
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/