[英]Decimal is ignored in type=“number” when there are no numbers after it
I have a fiddle here that removes the last character from the input when the user types .
我这里有一个小提琴,当用户键入时,该小提琴从输入中删除了最后一个字符
.
However it seems .length
ignores the decimal when there are no numbers after it. 但是,
.length
后面没有数字时,似乎忽略了小数。
For example: currently when the user types 20.
, it will be cut to 2
instead of 20
. 例如:当前,当用户键入
20.
,它将被剪切为2
而不是20
。
How can I detect this and remove the decimal once the user types it? 用户输入后,如何检测到并删除小数?
I think your requirement is to ignore it when a user types the period. 我认为您的要求是在用户键入句点时忽略它。
So all you need is this (notice the event is on keydown
and not keyup
): 因此,您所需
keydown
就是这个(注意,该事件是在keydown
而不是keyup
):
$('input').on('keydown', function(e) {
if (e.keyCode == 190 || e.keyCode == 110){
e.preventDefault();
}
});
Or, even easier, attribute the input so it only takes whole numbers : 或者,甚至更容易地, 将输入归因于输入,使其仅取整数 :
<input type="number" min="0" step="1"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.