[英]AngularJS form not submitting when pressing enter
我正在尝试在用户按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"...>
替换<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进行路由。
感谢您的任何意见,非常感谢。
把这一个
<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
而不是使用Button <input type ="submit" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.