按Enter键时AngularJS表单不提交

时间:2015-07-08 11:48:18

标签: javascript angularjs forms

我试图在用户按Enter键时提交登录表单。 单击"登录"按钮时表单完全正常,但按Enter键不起作用,此外,会导致奇怪的行为:

  • ng-submit关联的功能未执行
  • 按Enter
  • 后,再也不会再显示错误消息(登录失败)

这是我的标记:

<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进行路由。

感谢任何意见,非常感谢。

2 个答案:

答案 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" />