Angularjs 獲取checkbox所有選中的值

什麼也不多說了,直接上代碼

html代碼

<ul ng-repeat="d in data">
  <li><input type="checkbox" ng-model="select_all" ng-click="selectAll($event)"></li>
  <li><input type="checkbox" ng-checked="isSelected(d.id) || select_all" ng-click="updateSelection($event, d.id)"></li>
</ul>



js代碼

    $scope.data = {
      id: 1,
      id: 2,
      id: 3,
      id: 4
    }
    $scope.selected = [];
    var updateSelected = function(action, id) {
      if(action == 'add' & $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
      if(action == 'remove' && $scope.selected.indexOf(id) != -1) vm.selected.splice($scope.selected.indexOf(id), 1);
    };

    vm.updateSelection = function($event, id) {
      var checkbox = $event.target;
      var action = (checkbox.checked ? 'add' : 'remove');
      updateSelected(action, id);
    };

    vm.isSelected = function(id) {
      return $scope.selected.indexOf(id) >= 0;
    };   /*checkbox選中*/

    $scope.selectAll = function() {
      $scope.selected = []
      if($scope.select_all) {
        angular.forEach(vm.data.list, function (i) {
          $scope.selected.push(i.id);
        })
      }
    };



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章