vendredi 26 juin 2015

Select only two checkbox

I have a checkbox list and I want that the user be able to select just two of them and I want to get the value of the selected ones. I tried this code HTML

<input type="checkbox" value={{item.name}} id={{item.name}} ng-click="toggleSelection(item.name)">

JS

$scope.toggleSelection = function toggleSelection(id) {
    if(typeof $scope.firstSelectedService === 'undefined'){
    $scope.firstSelectedService=id;
    }
    else
    if(typeof $scope.secondSelectedService === 'undefined'){
    $scope.secondSelectedService=id;
    }
    else
    if($scope.firstSelectedService === id){
    window.alert("test11");
    $scope.firstSelectedService='';
    }
    else
    if($scope.secondSelectedService === id){
    window.alert("test2");
    $scope.secondSelectedService='';
    }
};

if the user select a third one I want to delete the old value of secondSelectedService and if he de-select a checked one, I want to delete it's value. I have a trouble when deselecting one, I don't see the alert box with "test1" or "test2", I see it only if I select the checkbox again. I only want to ask if it's a good way to do it like this ? it don't really looks clean.




Aucun commentaire:

Enregistrer un commentaire