如何立即获取复选框值

时间:2014-07-03 01:25:35

标签: javascript html angularjs

我正试图在Angular中检查复选框

我有类似

的东西
<div class="checkbox" ng-repeat="item in items">
     <input type="checkbox" ng-model="test[item.id]" ng-click="getID()" ng-checked="checkAll"/> {{item.Name}}
</div>

JS

$scope.getID = function(id){
    console.log(id)  -> it always get the id and I only want to get id when the checkbox is  
                     -> chekced
}

我只想在选中复选框时获取ID。我怎么做?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

您可以通过额外参数$scope.getID()更改isChecked以检查项目是否已被选中。另一件需要注意的事情是,你的html缺少函数所需的参数来获取ID

e.g。

<强>的Javascript

$scope.getID = function(id, isChecked) {
  if(isChecked) {
    console.log('ID:', id, 'is checked');
  } else {
    console.log('ID:', id, 'is unchecked');
  }
}

<强> HTML

改变这个:

<div class="checkbox" ng-repeat="item in items">
     <input type="checkbox" ng-model="test[item.id]" ng-click="getID()" ng-checked="checkAll"/> {{item.Name}}

到此:

<div class="checkbox" ng-repeat="item in items">
     <input type="checkbox" ng-model="test[item.id]" ng-click="getID(item.id, test[item.id])" ng-checked="checkAll"/> {{item.Name}}
</div>