2016-07-07 122 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> 

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

請幫幫我。

回答

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型號名稱。它應該是相同的..

兩個單選按鈕值將被「未定義」,如果你沒有選擇他們..

要檢查是否選擇了它,你可以使用下面的

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