检查选择了哪个单选按钮

问题描述:

我需要选中女巫单选按钮。检查选择了哪个单选按钮

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

如何在angularjs中检查单选按钮是否被选中? 我也试过了,但是对我来说不起作用。

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

请帮帮我。

<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型号名称。它应该是相同的..

两个单选按钮值将被“未定义”,如果你没有选择他们..

要检查是否选择了它,你可以使用下面的

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