简体   繁体   English

如果小数点后面没有数字,则在type =“ number”中忽略小数点

[英]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.

相关问题 检查小数点前的4个数字和小数点后的1个数字 - Check for 4 numbers before decimal and 1 number after decimal 在千数后添加逗号而不是十进制数 - Add Comma after thousand number not for decimal numbers Javascript将十进制数字格式转换为十进制后的树编号 - Javascript format a decimal number to tree numbers after decimal 名称=&#39;&#39;的无效表单控件无法使用类型=数字和十进制数字聚焦 - An invalid form control with name='' is not focusable with type=number and decimal numbers 限制输入数字小数点前后的数字 - Limit numbers before and after decimal point on input number jQuery Bugs ?? 两个数字相乘后的长十进制数 - Jquery Bugs?? Long decimal number after two numbers multiply 如何允许用户在小数点前输入一定数量的输入值,在小数点后键入一定数量的输入值 - How to allow user to type in certain number of input value before decimal and certain number of input value after decimal Ember 输入类型数字只允许在 Decimal 后输入 2 位数字 - Ember input type number to allow only 2 digits after Decimal Number指令支持十进制数 - Number directive to support decimal numbers 使用JS更改字体大小时忽略十进制 - Decimal ignored when changing font size with JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM