input的类型为number,限制输入的数字位数

当input的type为text的时候,使用maxlength可以有效限制输入长度。 

<input type="text"  maxlength="5" />   

但是当type为number的时候,设置maxlength限制输入长度就会失效,长度可以无限输入。

 <input type="number"  maxlength="5" />

解决方法:

<input type="number" oninput="if(value.length>5)value=value.slice(0,5)" />

上述即为使用oninput方法监听用户输入行为,当值得长度大于5的时候,用slice方法截取前5位的数字。

截取长度可自行设置

来源:http://blog.csdn.net/malan_wpf/article/details/50788275

原文地址:https://www.cnblogs.com/yuanxinru321/p/7426988.html