gpt4 book ai didi

javascript - 错误:Cannot call method 'reverse' of null

转载 作者:行者123 更新时间:2023-11-28 21:11:11 25 4
gpt4 key购买 nike

我在控制台(Ctrl+J)中出现错误:

Uncaught TypeError: Cannot call method 'reverse' of null

在下面的代码中:

///normal number formatting/////////////////////////////////////////////////////////////////////////////////////////    
$(document).delegate('input.numeric:text', 'keyup', function () {
$val = $(this).val().match(/[0-9]/g).reverse().join("").match(/[0-9]{1,3}/g).join(",").match(/./g).reverse().join("");
$(this).val($val)
});

如何修复这个错误?

最佳答案

错误表明该部分返回null:

$(this).val().match(/[0-9]/g)

该值不包含数字。我建议您验证:

$(document).delegate('input.numeric:text', 'keyup', function () {
$val = $(this).val().match(/[0-9]/g);

if ($val) {
$(this).val($val.reverse().join("")
.match(/[0-9]{1,3}/g).join(",")
.match(/./g).reverse().join(""));
}
});

关于javascript - 错误:Cannot call method 'reverse' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8613726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com