【问题标题】:Cordova Input type number maxlength not working in android?Cordova 输入类型号 maxlength 在 android 中不起作用?
【发布时间】:2016-07-05 22:11:51
【问题描述】:

我想显示 android 数字键盘并应用最大限制 2。 我用过<input type="number" min="0" max="99"/>。此代码仅适用于浏览器,不适用于安卓设备。

有没有人知道适合这种情况的解决方案?

【问题讨论】:

    标签: javascript jquery angularjs html cordova


    【解决方案1】:
    <input type="number" min="0" max="99"/> 
    

    非常适合网络浏览器。

    在 angularjs 中

    <input type="tel" ng-model="value"/>
    

    必须使用,这给了一个带有数字和符号的键盘。

    利用 angularjs 中的 $watch 的概念,可以实现最大限制和作为数字的输入类型。

    如果新值长度大于 2,则设置文本字段的值 到旧值

    $scope.$watch('value', function(newVal, oldVal) {
       if(newVal.length > 2) {       
          $scope.value = oldVal;
       }
    }
    

    以下是整数的正则表达式

     var onlyNumbers = /^\d+$/;
    

    如果新值与正则表达式匹配,则返回一个 数组,否则为空。如果返回类型为 null 则为 text 字段设置为旧值

      var r = newVal.match(onlyNumbers);
        if(r===null){
          $scope.value = oldVal;
        }
    

    完整的解决方案包含在下面的 plunker 链接中:

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

    【讨论】:

    • 输入 type="tel" 使用最大长度。但是我们想要输入类型=“数字”的解决方案。在这里 maxlength 没有得到应用。
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 2021-01-02
    • 2017-08-28
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多