0

In my HTML form there are 5 check boxes. How can I check which check boxes are checked or not at the time of the form submission?

3 Answers 3

1

I am using this little piece of code. Feel free to take it.

In your controller:

$scope.sentinel = [];

$scope.toggleSelection = function(value) {              
            // Bit trick, equivalent to "contains"
            if (~$scope.sentinel.indexOf(value)) {
                var idx = $scope.sentinel.indexOf(value);
                $scope.sentinel.splice(idx, 1);
                return;
            }

            $scope.sentinel.push(value);
        };

Then in your HTML:

<span ng-repeat="key in $scope.yourarray">
    <md-checkbox style="margin-left:30px;" aria-label="Select item"
    ng-click="toggleSelection(key)"><span>{{ key }}</span></md-checkbox>
    <br/>
</span>

This allows you to have an array of any size and using the sentinel array to register already checked values. If you check again a box, the toogleSelection will prevent you from adding again a value.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the Checklist-model of Angular.js. It is very useful:

var app = angular.module("app", ["checklist-model"]);
app.controller('Ctrl1', function($scope) {
  $scope.roles = [
    'guest', 
    'user', 
    'customer', 
    'admin'
  ];
  $scope.user = {
    roles: ['user']
  };
  $scope.checkAll = function() {
    $scope.user.roles = angular.copy($scope.roles);
  };
  $scope.uncheckAll = function() {
    $scope.user.roles = [];
  };
  $scope.checkFirst = function() {
    $scope.user.roles.splice(0, $scope.user.roles.length); 
    $scope.user.roles.push('guest');
  };
  $scope.showCheckedOnes = function() {
    var checkedBoxes = "";
    for (var i = 0; i < $scope.user.roles.length; i++) {
      checkedBoxes += $scope.user.roles[i] + " ";
    }
    alert(checkedBoxes);
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://vitalets.github.io/checklist-model/checklist-model.js"></script>

  <div ng-app="app">
    <div ng-controller="Ctrl1">

      <label ng-repeat="role in roles">
        <input type="checkbox" checklist-model="user.roles" checklist-value="role"> {{role}}
      </label>

    <div>
  <div>
  <button ng-click="checkAll()" style="margin-right: 10px">Check all</button>
  <button ng-click="uncheckAll()" style="margin-right: 10px">Uncheck all</button>
  <button ng-click="checkFirst()" style="margin-right: 10px">Check first</button>
  <button ng-click="showCheckedOnes()" style="margin-right: 10px">Show checked ones</button>

1 Comment

Thank you for spending your valuable time for my question.
0

You can define the array value and map to html.

$scope.checkbox = [];

HTML

<input ng-model="checkbox[0] type="checkbox" />
<input ng-model="checkbox[1] type="checkbox" />
<input ng-model="checkbox[2] type="checkbox" />
<input ng-model="checkbox[4] type="checkbox" />

Or you can define checkbox as object and change checkbox[index] to checkbox[name].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.