我想显示 Android 数字键盘并应用最大限制 2。
我用过<input type="number" min="0" max="99"/>
。此代码仅适用于浏览器,不适用于 Android 设备。
有人知道针对这种情况的合适解决方案吗?
最佳答案
<input type="number" min="0" max="99"/>
非常适合网络浏览器。
在 AngularJS 中
<input type="tel" ng-model="value"/>
必须使用,这提供了带有数字和符号的键盘。
利用angularjs中的$watch概念,可以实现最大限制和数字输入类型。
If the new value length is more than 2 then value of text field is set to old value
$scope.$watch('value', function(newVal, oldVal) {
if(newVal.length > 2) {
$scope.value = oldVal;
}
}
下面是整数的正则表达式
var onlyNumbers = /^\d+$/;
If the new Value matches the regular expression then it returns an array or else null. If the return type is null then value of text field is set to old value
var r = newVal.match(onlyNumbers);
if(r===null){
$scope.value = oldVal;
}
完整的解决方案包含在下面的 plunker 链接中:
关于javascript - Cordova 输入类型数字 maxlength 在 android 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38145866/