2
如果用户按Enter键,我需要在字段之间切换焦点。如果两个字段(登录& pwd)都不为空,则调用$ scope.submit。问题是如何在这两个领域之间切换焦点?我可以用document.getElementByid
来做,但是如果没有id attr?我如何获得ng-model的angularjs html标签来设置焦点?在Angularjs中的字段之间切换焦点
JS:
app.controller('loginCtrl', function($scope, $location, $rootScope,$http){
$scope.username = '';
$scope.password = '';
$scope.enter=function(){
if ($scope.username!="" && ($scope.password=="" || $scope.password==undefined)) document.getElementById("password").focus();
else if($scope.password!="" && ($scope.username=="" || $scope.username==undefined)) document.getElementById("username").focus();
else if($scope.password!="" && $scope.username!="") $scope.submit();
};
$scope.submit=function(){
var data={};
data.login=$scope.username;
data.pwd=$scope.password;
$http.post("authorization.php", data).then(function success (response) {
console.log(response);
if (response.data['loggin'] === true) {
$rootScope.loggedIn = true;
$location.path('/dashboard');
$rootScope.name = response.data['name'];
}
else {
$scope.username = '';
$scope.password = '';
}
},function error (response){
console.log(response);
}
);
}
});
HTML:
<div ng-controller="loginCtrl" class="login">
<form action="/" id="myLogin" method="post">
<input type="text" name="username" id="username" ng-model="username" placeholder="Username" required="required" class="input-txt" ng-keyup="$event.keyCode == 13 ? enter() : null"><br>
<input type="password" name="password" id="password" ng-model="password" placeholder="Password" required="required" class="input-txt" ng-keyup="$event.keyCode == 13 ? enter() : null"><br>
<br>
<button type="button" ng-click="submit()" class="btn btn--right">Login</button>
</form>
</div>
@Fran在https://stackoverflow.com/questions/41283650/how-can-i-move-focus-to-next-control-on-key-enter-in-angular-2中的问题是关于Angular/Angular4,这个问题与Angular 1.x有关 –