0

when i go to page i need always checked somecheck boxes like software or harware.if any one selected then only submit button will visible.if nothing is selected submit button should disable and alert should come please select some services

function Test1Controller($scope) {
  var storeid = window.localStorage.getItem("storeid");
  var serverData = ["software", "hardware", "Accessories"];
  $scope.items = [];

  for (var i = 0; i < serverData.length; i++) {
    var modal = {
      name: serverData[i],
      selected: i === 0 ? true : false,

    };
    $scope.items.push(modal);
  }

  $scope.check = function() {
    var checkedItems = [];
    for (var i = 0; i < $scope.items.length; i++) {
      if ($scope.items[i].selected) {
        checkedItems.push($scope.items[i].name);
      }
    }
    console.log(checkedItems);
  }


}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>

<div ng-app>
  <div ng-controller="Test1Controller">
    <div ng-repeat="item in items">
      <input type="checkbox" ng-model="item.selected" />{{item.name}}
    </div>
    <input type="button" name="submit" value="submit" ng-click="check()" />
  </div>
</div>

3
  • Change selected: false in for to selected: i === 0 ? true : false, to set first checkbox checked by default Commented Aug 6, 2015 at 6:31
  • how can i validate here everytime only one checkbox is selected then only submit but will submit visible Commented Aug 6, 2015 at 6:39
  • If they can only select one option, you should consider using radiobuttons instead of checkboxes. Checkboxes are when you have multiple options and you can select multiple 'answers'. Give your radiobuttons the same name and they will be able to choose just one radiobutton. See w3schools.com/html/tryit.asp?filename=tryhtml_radio for example. Commented Aug 6, 2015 at 7:17

1 Answer 1

1
function Test1Controller($scope) {
  var storeid = window.localStorage.getItem("storeid");
  var serverData = ["software", "hardware", "Accessories"];
  $scope.items = [];

  for (var i = 0; i < serverData.length; i++) {
    var selectVal = false;
    if(serverData[i] === "hardware"){
        selectVal = true;
    }
    var modal = {
      name: serverData[i],
      selected: selectVal

    };
    $scope.items.push(modal);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I have been struggling to resolve this issue for 2 days

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.