gpt4 book ai didi

javascript - 在 Angularjs 中的字段之间切换焦点

转载 作者:行者123 更新时间:2023-11-30 11:35:39 25 4
gpt4 key购买 nike

<分区>

如果用户按下 enter,我需要在字段之间切换焦点。并且如果两个字段(登录名和密码)都不为空 $scope.submit 被调用。问题是如何在这两个字段之间切换焦点?我可以使用 document.getElementByid 来实现,但是如果没有 id 属性呢?如何使用 ng-model 进入 angularjs html 标签来设置焦点?

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>

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com