禁用该按钮,直到输入字段填满不起作用

时间:2018-03-21 04:09:40

标签: html angularjs twitter-bootstrap

我想禁用提交按钮,直到使用angularjs / bootstrap在页面中填充所有输入字段。我尝试使用ng-disabled="myForm.$invalid",但它似乎无法正常工作。有什么输入吗?

Demo

HTML代码

<form name="myForm" ng-controller="ExampleController">
   <div>Select Color : </div>
      <label name="team" ng-repeat="color in colorNames" class="checkbox-inline">
                        <input type="checkbox" name="color" value="{{color}}" ng-checked="selectedColor.indexOf(color) > -1" ng-click="userSelection(color)" required> {{color}}
                  <br>  </label><br>
                <div ng-show="myForm.$submitted || myForm.color.$touched">
                    <p class="error-mesg" ng-show="myForm.color.$error.required">Please Select the color</p>
                </div>
            <div class="">
                <div style="color: black;">Username : </div>
               <input type="text" name="user" value="" required>
                <div ng-show="myForm.$submitted || myForm.user.$touched">
                    <p class="error-mesg" ng-show="myForm.user.$error.required">The Username is required</p>
                </div>
            </div>
            <br>
  <button type="submit" class="btn btn-primary" ng-click="submitForm(myForm)" ng-disabled="myForm.$invalid">Submit</button>
</form>

5 个答案:

答案 0 :(得分:2)

我认为您错过了为复选框和文本框添加ng-model。

请检查以下代码。

&#13;
&#13;
<meta charset="UTF-8">
  <title>Example - example-checkbox-input-directive-production</title>
   <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
   
</head>
<body ng-app="checkboxExample">
  <script>  
  angular.module('checkboxExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      
      $scope.colorNames = ['RED', 'BLUE', 'BLACK'];
        $scope.selectedColor = [];
        $scope.userSelection = function userSelection(team) {
            var idx = $scope.selectedColor.indexOf(team);
           if (idx > -1) {
                $scope.selectedColor.splice(idx, 1);
            }
            else {
                $scope.selectedColor.push(team);
            }
        };
        $scope.submitForm = function(){
             if ($scope.selectedColor != "" && $scope.selectedColor != undefined && $scope.user != "" && $scope.user != undefined) {
          alert("all fields are entered");
            }else{
               
            }
        } 
        
    
    }]);
</script>
<form name="myForm" ng-controller="ExampleController">
   <div>Select Color : </div>
      <label name="team" ng-repeat="color in colorNames" class="checkbox-inline">
                        <input type="checkbox" name="color" ng-model="colouer" value="{{color}}" ng-checked="selectedColor.indexOf(color) > -1" ng-click="userSelection(color)" required> {{color}}
                  <br>  </label><br>
                <div ng-show="myForm.$submitted || myForm.color.$touched">
                    <p class="error-mesg" ng-show="myForm.color.$error.required">Please Select the color</p>
                </div>
            <div class="">
                <div style="color: black;">Username : </div>
               <input type="text" name="user" value="" ng-model="username" required>
                <div ng-show="myForm.$submitted || myForm.user.$touched">
                    <p class="error-mesg" ng-show="myForm.user.$error.required">The Username is required</p>
                </div>
            </div>
            <br>
  <button type="submit" class="btn btn-primary" ng-click="submitForm(myForm)" ng-disabled="myForm.$invalid">Submit</button>

   

 </form>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你写错了。请写下ng-disabled =&#34;!myForm。$ invalid&#34;。另外,在html中编写{{myForm。$ invalid}}会显示其当前值,在您的情况下这是错误的。

    <form name="myForm" ng-controller="ExampleController">
       <div>Select Color : </div>
          <label name="team" ng-repeat="color in colorNames" class="checkbox-inline">
                            <input type="checkbox" name="color" value="{{color}}" ng-checked="selectedColor.indexOf(color) > -1" ng-click="userSelection(color)" required> {{color}}
                      <br>  </label><br>
                    <div ng-show="myForm.$submitted || myForm.color.$touched">
                        <p class="error-mesg" ng-show="myForm.color.$error.required">Please Select the color</p>
                    </div>
                <div class="">
                    <div style="color: black;">Username : </div>
                   <input type="text" name="user" value="" required>
                    <div ng-show="myForm.$submitted || myForm.user.$touched">
                        <p class="error-mesg" ng-show="myForm.user.$error.required">The Username is required</p>
                    </div>
                </div>
                <br>
{{myForm.$invalid}}
<button type="submit" class="btn btn-primary" ng-click="submitForm(myForm)" ng-disabled="!myForm.$invalid">Submit</button>
    </form>

答案 2 :(得分:1)

ng-model添加到您的输入字段

  <input type="text" name="user" value="" ng-model="user" required>

答案 3 :(得分:0)

你还需要做两件事,   1.对所有输入使用ng-required而不是必需。并添加ng-model      对于所有输入。   2.对于复选框,您需要检查是否至少有一个复选框      选择。如果选择了一个,则可以使ng-required = false。      为此,您可以检查selectedColor的长度是否更大      比0。

PFB修改后的代码,

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-checkbox-input-directive-production</title>
  <script src="//code.angularjs.org/snapshot/angular.min.js"></script>

</head>
<body ng-app="checkboxExample">
  <script>  
  angular.module('checkboxExample', [])
    .controller('ExampleController', ['$scope', function($scope) {

      $scope.colorNames = ['RED', 'BLUE', 'BLACK'];
        $scope.selectedColor = [];
        $scope.userSelection = function userSelection(team) {
            var idx = $scope.selectedColor.indexOf(team);
          if (idx > -1) {
                $scope.selectedColor.splice(idx, 1);
            }
            else {
                $scope.selectedColor.push(team);
            }
        };
        $scope.submitForm = function(){
            if ($scope.selectedColor != "" && $scope.selectedColor != undefined && $scope.user != "" && $scope.user != undefined) {
          alert("all fields are entered");
            }else{

            }
        } 


    }]);
</script>
<form name="myForm" ng-controller="ExampleController">
  <div>Select Color : </div>
      <label name="team" ng-repeat="color in colorNames" class="checkbox-inline">
                        <input type="checkbox" ng-model="checked" name="color" ng-required="selectedColor.length==0" value="{{color}}" ng-click="userSelection(color)" > {{color}}
                  <br>  </label><br>
                <div ng-show="myForm.$submitted || myForm.color.$touched">
                    <p class="error-mesg" ng-show="myForm.color.$error.required">Please Select the color</p>
                </div>
            <div class="">
                <div style="color: black;">Username : </div>
              <input type="text" name="user" value="" ng-model="user" ng-required="true">
                <div ng-show="myForm.$submitted || myForm.user.$touched">
                    <p class="error-mesg" ng-show="myForm.user.$error.required">The Username is required</p>
                </div>
            </div>
            <br>
  <button type="submit" class="btn btn-primary" ng-click="submitForm(myForm)" ng-disabled="myForm.$invalid">Submit</button>



</form>
</body>
</html>

答案 4 :(得分:0)

我做了两处改动:

  1. 为您的字段添加ng-model
  2. 在复选框中,我将模型名称更改为ng-model="color_$index"
  3. $ index 获取数组的索引,它有助于分别识别每个复选框。

    <form name="myForm" ng-controller="ExampleController">
            <div>Select Color : </div>
                    <label name="team" ng-repeat="color in colorNames" class="checkbox-inline">
                        <input type="checkbox" name="color" ng-model="color_$index" value="{{color}}" ng-checked="selectedColor.indexOf(color) > -1" ng-click="userSelection(color)" required> {{color}}
                        <br>
                    </label>
                    <br>
                    <div ng-show="myForm.$submitted || myForm.color.$touched">
                        <p class="error-mesg" ng-show="myForm.color.$error.required">Please Select the color</p>
                    </div>
                    <div class="">
                        <div style="color: black;">Username : </div>
                        <input type="text" name="user" ng-model="username" value="" required>
                        <div ng-show="myForm.$submitted || myForm.user.$touched">
                            <p class="error-mesg" ng-show="myForm.user.$error.required">The Username is required</p>
                        </div>
                    </div>
                    <br>
            <button type="submit" class="btn btn-primary" ng-click="submitForm(myForm)" ng-disabled="myForm.$invalid">Submit</button>
            </form>