2016-07-07 4 views
0

마녀 라디오 버튼을 선택해야합니다.어떤 라디오 버튼이 선택되어 있는지 확인하십시오.

<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> 

라디오 버튼이 선택되면 어떻게 할 수 있습니까? 나는 그것을 시도했지만 나에게 그것은 효과가 없다.

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"); 
    } 
} 

도와주세요.

답변

1
<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> 

이 경우 동일한 이름이지만 두 개의 ng-model 이름이 있습니다. 그것은 .. 동일해야합니다

모두 당신이 그들을 선택하지 않은 경우 값이 '정의되지 않은'됩니다 라디오 버튼 ..이 선택되어 있으면

는 다음과 같은

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");   
} 
를 사용할 수 있습니다 확인하려면
+0

그래서 이것을 만들 필요가 있습니까? – MrPokemon

+0

$ scope.radioNein = false; $ scope.radioJa = true; – MrPokemon

+0

거의 작동합니다. 'Ja'버튼은 작동하지만 'Nein'버튼은 작동하지 않습니다. – MrPokemon

관련 문제