MrPokemon
MrPokemon

Reputation: 99

check which radio button is selected

I need to check witch radio button is selected.

<div class="cClearFloat cInputSpace cRadioAdmin">
    <label class="cLabelJa"><input type="radio" value="Ja" name="Admin" ng-model="radioJa">Ja</label>
    <label><input type="radio" name="Admin" value="Nein" ng-model="radioNein">Nein</label>
</div>

How can i check in angularjs if the radio button is selcted? i tried it too but by me it's not working.

if ($scope.radioJa.checked == true) {
       $scope.saveUser();
       $scope.currentUser.isAdmin = 'Ja';
    } else if($scope.radioNein == true) {
       $scope.currentUser.isAdmin = 'Nein';
       $scope.saveUser();
    } else {
        alert("Bitte füllen Sie alle Felder korrekt aus!", "Fehler");
    }
}

please help me.

Upvotes: 0

Views: 129

Answers (1)

SuperNova
SuperNova

Reputation: 27446

<label class="cLabelJa"><input type="radio" value="Ja" name="Admin" ng-model="radioJa">Ja</label>
<label><input type="radio" name="Admin" value="Nein" ng-model="radioNein">Nein</label>

In this case you have same name, but it has two difference ng-model name. It should be same..

Both the radio buttons value will be 'undefined', if you have not selected them..

To check if it is selected, you can use the following

if ( !$scope.radioJa ){
     alert('comes here if the radio button is not selected');        
}
if ( $scope.radioJa ){
     alert('comes here if the radio button is selected'); 
     alert("and the value of $scope.radioJa will be `Ja` if the 1st radio button is selected");        
}

Upvotes: 1

Related Questions