search

Home  >  Q&A  >  body text

html5 - angularjs怎么实现checkbox选中触发事件?

html代码:

<input type="checkbox" ng-click="sub(1)">选择



<p ng-show="showType==1" class="btn">
      <button type="submit">提交申请</button>
        </p>


        <p ng-show="showType==2" class="btn">
      <button type="submit">提交申请</button>
        </p>

js代码:

var app = angular.module('test', []);
    app.controller('chkCtrl', function($scope){
         $scope.showType=1;
        $scope.sub = function(type){
            if(type==1){
         $scope.showType =2;
            }
            else if( $scope.showType ==2){
                $scope.showType =1;
            }
            };

这代码点击勾选的时候有效,但是取消勾选就没效了。请问要怎么样修改?

巴扎黑巴扎黑2784 days ago883

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 11:30:19

    Remove the parameters of

    $scope.sub(), and then add a sentence in the method body to negate $scope.showType

    HTML

    <input type="checkbox" ng-click="sub()">选择
    
    
    
    <p ng-show="showType==1" class="btn">
      <button type="submit">提交申请</button>
    </p>
    
    
    
    
    
    <p ng-show="showType!=1" class="btn">
      <button type="submit">提交申请</button>
    </p>
    
    
    

    JS

    var app = angular.module('test', []);
    app.controller('chkCtrl', function($scope){
        $scope.showType=1;
        $scope.sub = function(){
            $scope.showType=!$scope.showType;
        }
    };
    

    Is this what you mean?

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 11:30:19

    Your sub parameter is always 1, of course there will be no change

    reply
    0
  • Cancelreply