javascript - Angularjs - 如果任何值超过 ng-repeat 元素值,如何禁用提交按钮

标签 javascript angularjs ng-submit angularjs-ng-disabled

我有这样类型的程序。

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.records = [
    {
      "Amt" : "500",
      
    },
    {
      "Amt" : "800",
     
    },
    {
      "Amt" : "1580",
    },
    {
      "Amt" : "1122",
    }
  ]
  
  $scope.value=function(d, x)
  {
    x.balance = x.Amt - d;
    if(d > x.Amt){
    $scope.isExceeded = true;
  } else {
    
    $scope.isExceeded = false;
  }  
  }
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp">

<table ng-controller="myCtrl" border="1">
  <tr>
  <td>Amt</td>
  <td>Balance</td> 
  <td>Entered Amt</td>
  <td ng-if="error"> Error</td>
</tr>
<tr ng-repeat="x in records">
  <td>{{x.Amt}}</td>
  <td>{{x.balance}}</td>
   <td><input type="text" ng-model="d" ng-change="value(d, x)"></td>  
  <td ng-if="d > x.Amt" ng-model="error">Please enter less than {{x.Amt}}</td>
</tr>
<tr>
  <td colspan="4"><input type="button" value="Submit" ng-disabled="isExceeded"></td>
</tr>
</table>

</body>
</html>

我想要的是,如果任何输入的 Amt 值超过 Amt 值,则提交按钮应变为禁用。

我不知道如何实现这种类型的条件。我是 angularjs 的新手。

最佳答案

您可以使用变量 isExceeded 并在 Angular 标记中使用 ng-disable 指令来禁用该按钮。

<body ng-app="myApp">   
   <table ng-controller="myCtrl" border="1">
     <tr>
       <td>Amt</td>
       <td>Balance</td> 
       <td>Entered Amt</td>
       <td ng-if="error"> Error</td>
     </tr>
     <tr ng-repeat="x in records">
       <td>{{x.Amt}}</td>
       <td>{{x.balance}}</td>
       <td><input type="text" ng-model="d" ng-change="value(d, x)"></td>  
       <td ng-if="d > x.Amt" ng-model="error">Please enter less than {{x.Amt}}</td>
     </tr>
     <tr>
       <td colspan="4" ng-disable="isExceeded"><input type="button" value="Submit"></td>
     </tr>
   </table>    
</body>

并在 Angular Controller 中检查它,例如

$scope.value=function(d, x)
{
  if(d > x.Amt){
    $scope.isExceeded = true;
  } else {
    x.balance = x.Amt - d;
    $scope.isExceeded = false;
  }  
}

关于javascript - Angularjs - 如果任何值超过 ng-repeat 元素值,如何禁用提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42083751/

相关文章:

javascript - 在highcharts ng中动态加载数据

javascript - Angular js表单在不应该提交的时候提交

angularjs - 当提交按钮位于此表单之外时,如何使用 angularJs 验证表单?

angularjs - 在单元测试中提交 Angular 形式

javascript - div onclick函数更改正文背景图像

javascript - 如何停止使用jsp或php为两个标签选择重复选项?

javascript - Angular 插值无法正常工作?

javascript - jQuery.datepicker.formatDate 和时区偏移量

javascript - 在angularjs中的$scope成员函数内引用函数内的变量

angularjs - 通过最新输入加载 json - Angularjs