简体   繁体   中英

How can I check if the text of an input field is “yes” or “no”?

I want to check if the text from the input field isAdmin is "yes" or "no". Is it possible to do this with ng-click ?

            <div class="cClearFloat cInputSpace">
                    <input placeholder="login" ng-model="currentUser.login">
                </div>
                <div class="cClearFloat cInputSpace">
                    <input placeholder="Vorname" ng-model="currentUser.Vorname">
                </div>
                <div class="cClearFloat cInputSpace">
                    <input placeholder="Nachname" ng-model="currentUser.Nachname">
                </div>
                <div class="cClearFloat cInputSpace">
                    <input placeholder="password" ng-model="currentUser.password">
                </div>
                <div class="cClearFloat cInputSpace">
                    <input placeholder="Admin" ng-model="currentUser.isAdmin">
                </div>
                <div class="cClearFloat cButtonsUser">
                    <button class="cButtonSpeichern" ng-click="saveUser()">Speichern</button>
                    <button class="cButtonAbbrechen" ng-click="isShownUser= false">Abbrechen</button>
                </div>
            </div>

These are the input fields and when I press on the button "Speichern", I want to check it.

What is the problem to check your inputs on ng-click handler?

if ($scope.currentUser.login && $scope.currentUser.login.toLowerCase() ==='yes') {
console.log('that is it');
}

UPDATED:

If you have time, Ill advice you to read about ng-model controller . Filds validation is his responosibility and it have a lot of tools for this.

You can check those values inside your saveUser() function. For example:

//Inside your controller:
$scope.saveUser = function () {
    if ($scope.currentUser.login && $scope.currentUser.login.toLowerCase() === 'yes') {
        //do something
    } else {
        //do something else/display error 
    }
}

You can check all values you need this way

I made it like this and it works!

JavaScript:

    $scope.showAlert = function() {
        //überprüfen ob die textfelder leer sind
        if ($scope.currentUser.isAdmin == 'Ja' | $scope.currentUser.isAdmin == 'ja') {
            $scope.isAdmin = "Ja";
            $scope.saveUser();
        } else if($scope.currentUser.isAdmin == 'Nein' | $scope.currentUser.isAdmin == 'nein') {
            $scope.isAdmin = "Nein";
            $scope.saveUser();
        } else {
            alert("Bitte geben Sie ein ob der Benutzer ein Admin ist oder nicht!", "Fehler");
        }
    }

HTML:

     <button class="cButtonSpeichern" ng-click="showAlert()">Speichern</button>

but thank you for your help guys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM