首页 > 解决方案 > 禁用输入字段 x 天

问题描述

假设我今天提交了我的表单,即 1 月 9 日,那么我希望我的表单文本字段在接下来的一个月内被禁用。这样用户每个月只能更新一次。

<input type="number" min="1" name="month" ng-model="year.month"
       class="form-control" required />

标签: javascriptangularjs

解决方案


我认为你使用 AngularJS 我看到了 ng-model。我使用伪代码,将当前月份放入一个变量中,然后在服务中比较用户选择的内容。比较附加布尔结果并放入 ng-disabled 中。

 
    
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.isChange = function() {
      var now = new Date();
      monthNow = now.getMonth();
      if ((!$scope.year) || ($scope.year.month != monthNow)) {
        $scope.check = false;
      } else {
        $scope.check = true;
      }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
<input type="number" min="1" name="month" ng-model="year.month"
           class="form-control" required ng-disabled="check" ng-change="isChange()" ng-init="isChange()"/>
</div>


推荐阅读