键盘事件中的javascript数字格式
我正在尝试在键盘输入事件中在输入字段中设置数字格式,但是我在浏览器的控制台上一直收到警告
无法解析指定的值"5,545",或超出范围.
我在输入字段中输入的值将被清除.请问如何解决该问题?
下面是处理数字格式的jquery代码段:
I am trying to format number in an input field on keyup event but I kept getting a warning on the console of my browser
The specified value "5,545" cannot be parsed, or is out of range.
and the value I have entered in the input field cleans up. Please what can be done to solve the problem?
Below is the jquery code snippet that handles the number formatting:
$('input[type="number"]').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/([0-9])([0-9]{2})$/, '$1.$2')
.replace(/\B(?=(\d{3})+(?!\d)\.?)/g, ",");
});
});
我在浏览器控制台中遇到的错误或警告是:
,显示此警告后,输入字段中的值将清除.请问哪里有问题,该怎么办?
Error or warning I get in the browser console is:
and the value in my input field cleans up after displaying this warning. Please what is wrong and what can be done?
导致错误的原因是,
,因为5,289
尝试将,
更改为.
What causes the error is the ,
since 5,289
try changing the ,
to a .
EX:
'5,289' => '5.289'