【问题标题】:angularjs text box must have max length digitsangularjs文本框必须有最大长度数字
【发布时间】:2018-05-29 11:43:01
【问题描述】:

我有 30 个文本框,第一个文本框必须有 9 个数字,第二个文本框必须有 3 个数字,在第一个文本框达到最大长度后,我有这样的代码,它会移动到下一个文本框。

我怎样才能做到这一点?

function numOnly() {
  var directive = {
      restrict: 'A',
      scope: {
          ngModel: '=ngModel'
      },
      link: link
  };
  return directive;

  function link(scope, element, attrs) {
    scope.$watch('ngModel', function (newVal, oldVal) {
        var arr = String(newVal).split('');
        if (arr.length === 0) return;
        if (arr.length === 1 && (arr[0] === '-' || arr[0] === '.')) return;
        if (isNaN(newVal)) {
            //scope.ngModel = oldVal;
            scope.ngModel = "";
        }
    });
}
<tr ng-repeat="cl in codelist" ng-if="$odd" >
    <td>{{$index+1}}</td>
    <td>
        <label ng-model="codelist[$index].CodeNumber">{{codelist[$index].CodeNumber}}</label>
    </td>
    <td ng-class="{ 'has-error' : codeForm.clHtnoA{{$index}}.$invalid && (codeForm.clHtnoA{{$index}}.$dirty || submitted) }">
        <input type="text" name="clHtnoA{{$index}}" ng-model="codelist[$index].HtnoA" num-only maxlength="9" ng-readonly="{{codelist[$index].Status}}" required class="form-control" />
        <span ng-show="codeForm.clHtnoA{{$index}}.$error.required && (codeForm.clHtnoA{{$index}}.$invalid.$dirty || submitted)" class="help-block">HallTicket is required.</span>
    </td>
    <td ng-class="{ 'has-error' : codeForm.clHtnoB{{$index}}.$invalid && (codeForm.clHtnoB{{$index}}.$dirty || submitted) }">
        <input type="text" name="clHtnoB{{$index}}" ng-model="codelist[$index].HtnoB" num-only maxlength="3" ng-change="findNameAB($index)" ng-readonly="{{codelist[$index].Status}}" required class="form-control" />
        <span ng-show="codeForm.clHtnoB{{$index}}.$error.required && (codeForm.clHtnoB{{$index}}.$invalid.$dirty || submitted)" class="help-block">3 no's only.</span>
    </td>
    <td>
        <span ng-model="codelist[$index].Sname">{{codelist[$index].Sname}}</span>
    </td>
</tr>

【问题讨论】:

  • 贴一些html代码

标签: angularjs


【解决方案1】:

指令是你的解决方案:

app.directive("range", [function() {
        return {
            restrict: "A",
            link: function(scope, elem, attrs) {
                var range = parseInt(attrs.limitTo);
                angular.element(elem).on("keypress", function(e) {
                    if (this.value.length == range) e.preventDefault();
                });
            }
        }
    }]);


<input type="text" ng-model="mybox1" range="9"/>

【讨论】:

    【解决方案2】:

    利用 ng-maxlength

    <div ng-app="myApp" ng-controller="myCtrl">
    <form>
            <input type="text" id="part1" ng-model="myObject.part1"  ng-maxlength = "9"/>
            <input type="text" id="part2" ng-model="myObject.part2"  ng-maxlength = "3" />
        </form>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多