cari

Rumah  >  Soal Jawab  >  teks badan

angular.js - angularjs如何判断checkbox选中2个

<!DOCTYPE html>
<html ng-app="fromApp">
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="angular.min.js"></script>
</head>
<body>
    <p ng-controller="formController">
        <p class="form-group">
            <form name="formData">
            <label class="checkbox-inline">
                <input type="checkbox" name="favoriteColors" value="red" ng-model="formData.favoriteColors.red"> Red
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" name="favoriteColors" value="blue" ng-model="formData.favoriteColors.blue"> Blue
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" name="favoriteColors" value="green" ng-model="formData.favoriteColors.green"> Green
            </label>
                <button ng-click="check()">提交</button>
            </form>
        </p>
    </p>
    <script>
    var app= angular.module('fromApp',[]);
        app.controller('formController',function($scope){
            $scope.fromData={};

            $scope.check=function(){

            }
        });
    </script>
</body>
</html>

如何实现点击提交当checkbox选中超过2个做出提示即可

曾经蜡笔没有小新曾经蜡笔没有小新2744 hari yang lalu596

membalas semua(2)saya akan balas

  • 给我你的怀抱

    给我你的怀抱2017-05-15 16:54:57

    form dan from ditulis dengan sangat mengelirukan

    js var app= angular.module('fromApp',[]);
            app.controller('formController',function($scope){
                $scope.fromData={};
    
                $scope.check=function(){
                    if($scope.fromData.favoriteColors){
                        var log = [];
                        angular.forEach($scope.fromData.favoriteColors,function(v){
                                if(v==true)
                                    this.push(v);
                        },log);
                        console.log(log.length);//length
                    }
                }
            });
    

    balas
    0
  • 巴扎黑

    巴扎黑2017-05-15 16:54:57

    Mengapa tidak menggunakan radio

    Jika anda benar-benar mahu melakukan ini, anda boleh

    $scope.check=function(){
    var checked=[];
    if(!$scope.formData.favoriteColors){
    pulangkan palsu;
    }
    if($scope.formData.favoriteColors.red)
    checked.push('red');
    if($scope.formData.favoriteColors.blue)
    checked.push('biru');
    if($scope.formData.favoriteColors.green)
    checked.push('hijau');

    if (checked.length>=2){
        ...
    }
    

    }`

    balas
    0
  • Batalbalas