我试图在用户按Enter键时提交登录表单。 单击"登录"按钮时表单完全正常,但按Enter键不起作用,此外,会导致奇怪的行为:
ng-submit
关联的功能未执行这是我的标记:
<form ng-submit="login()" class="login-form">
<div class="alert alert-danger" ng-class="{ 'display-hide': !showError }">
<button class="close" data-close="alert"></button>
<span> Login failed </span>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Username</label>
<input class="form-control form-control-solid placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="username" ng-model="username"/>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<input class="form-control form-control-solid placeholder-no-fix" type="password" autocomplete="off" placeholder="Password" name="password" ng-model="password"/>
</div>
<div class="form-actions">
<button class="btn btn-success uppercase">Login</button>
</div>
</form>
请注意,我尝试将<button...>
替换为<input type="submit"...>
,但也没有成功。
具有login() - 函数的相应控制器如下所示:
.controller('LoginCtrl', ['$rootScope', '$scope', ..., function LoginController ($rootScope, $scope, ...) {
$scope.showError = false;
$scope.login = function()
{
console.log("logging in");
$http({
url: $scope.apiEndpointLogin,
method: 'POST',
data: {'username': this.username, 'password': this.password}
}).then(function(response) {
$scope.showError = false;
[...]
}, function() {
$scope.showError = true;
});
};
}]);
我运行angular v1.4.0并使用angular-ui-router v.0.2.15进行路由。
感谢任何意见,非常感谢。
答案 0 :(得分:6)
放一个
<input type="submit" class="btn btn-success uppercase" value="Login" >
而不是
<button class="btn btn-success uppercase">Login</button>
表单提交按钮应该是提交类型。
编辑代码:
<form class="login-form" ng-submit="login()" >
<div class="alert alert-danger" ng-class="{ 'display-hide': !showError }">
<button class="close" data-close="alert"></button>
<span> Login failed </span>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Username</label>
<input class="form-control form-control-solid placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="username" ng-model="username"/>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<input class="form-control form-control-solid placeholder-no-fix" type="password" autocomplete="off" placeholder="Password" name="password" ng-model="password"/>
</div>
<div class="form-actions">
<input type="submit" class="btn btn-success uppercase" >
</div>
</form>
另外,请检查 LoginCtrl 是否位于表单DOM之上。
编辑代码:
请检查此Plunker
答案 1 :(得分:1)
而不是按钮使用<input type ="submit" />