javascript - Cordova 输入类型数字 maxlength 在 android 中不起作用?

标签 javascript jquery angularjs html cordova

我想显示 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 链接中:

https://plnkr.co/edit/WrZ3xKs9EcXOvCemuIck?p=preview

关于javascript - Cordova 输入类型数字 maxlength 在 android 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38145866/

相关文章:

javascript - 检查复选框是否选中 javascript 不起作用

jquery - 无法使用 jquery 设置隐藏输入值

javascript - 导航菜单未在浏览器中呈现

angularjs - 如何运行 Protractor

javascript - 停止事件的双重执行

javascript - Angular 下拉列表的问题 - 单击链接时动态选择一个选项

javascript - 在 Android 中通过 Javascript 隐藏/显示 WebView

jquery - 在 ipad 的情况下使位于文本框上的图像可点击(为 iOS 创建 HTML5 搜索输入类型)

angularjs - 如何使用 Angular js 获取 Wordpress 帖子

javascript - 嵌套 $http.get 函数