【问题标题】:Angular.js - Form submits when i enter return falseAngular.js - 当我输入返回 false 时提交表单
【发布时间】:2016-08-25 23:52:39
【问题描述】:

我正在练习我的 angular.js 和表单验证。我想这样做,如果变量 user_valid 和 pass_valid 为 false,则当我单击提交时表单不会提交。因此,通过调用 return false; 在 angularjs 之外编写代码时,我做得非常好。 但是在 angular.js 中工作时,插入 ng-submit='loginVal()',然后输入我的控制器:`

logApp.controller('logForm', function($scope, $http){
    user_valid = false;
    pass_valid = false;

    $scope.loginVal = function(){
        if (user_valid == false && pass_valid == false){
            return false;
            console.log('Submit Stopped');
        }
    }`

    ...

});

表单仍然提交,并显示在控制台Navigated to ~form-action url~。不知道为什么要提交。控制器中的其余功能与此功能无关,因此我将其排除在外。

HTML:

<form name='login' method="post" action="" ng-submit='loginVal()'>
        {% csrf_token %}
            <!-- LOGIN FORM -->
            <table id='show-table'>
                <tr>
                    <td width='20%'>Username</td>
                    <td width="80%">
                        <div class='col-md-12' id='userField'>
                            <input name='name' type="text" ng-model='username' ng-change='checkName()'/>
                            <div id='success'></div>
                            <div id='failure'></div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td>
                        <div class='col-md-12'>
                            <input name="pwd" type="password" ng-model='password' />
                        </div>
                    </td>
                </tr>
                    <td></td>
                    <td><input type="submit" value="Login" id='loginSubmit' disabled /></td>
                </tr>
            </table>

        </form>

【问题讨论】:

  • 你能发布html吗?
  • 顺便说一句,return 之后的任何代码都是无法访问/死代码。 console.log 没有用。
  • 好的,我会解决的。 @SmileApplications 我已经发布了 HTML。

标签: angularjs forms submit


【解决方案1】:

去掉form标签中的action和method属性

编辑 看看这个小提琴:https://jsfiddle.net/80Laf822/1/

<div ng-controller="FormController">
  <form name='loginForm' method="post" action="" ng-submit='loginVal()'>
    <!-- LOGIN FORM -->
    <table id='show-table'>
      <tr>
        <td width='20%'>Username</td>
        <td width="80%">
          <div class='col-md-12' id='userField'>
            <input name='name' type="text" ng-model='username' required/>
            <div id='success'></div>
            <div id='failure'></div>
          </div>
        </td>
      </tr>
      <tr>
        <td>Password</td>
        <td>
          <div class='col-md-12'>
            <input name="pwd" type="password" ng-model='password' required/>
          </div>
        </td>
      </tr>
      <tr>
        <td></td>
        <td>
          <input type="submit" value="Login" id='loginSubmit' ng-disabled="loginForm.$invalid" />
        </td>
      </tr>
    </table>

  </form>
</div>

您可以使用输入中的 required 属性和表单的属性 $invalid 来自动禁用或启用表单是否有效的按钮。你可以做所有类型的事情,比如patternsmax-lengthmin-lenght ...

【讨论】:

  • 我想把它当作一个表格来使用,但我正在努力做到如果用户名或密码无效(由user_valid=falsepass_valid=false表示),表格将不会提交。如果我删除操作和方法,当 user_valid 和 pass_valid 为 true 时,表单将不会提交。
  • 为什么不简单设置 ng-disabled="checkIfAuthIsValid()" 以便在用户名或密码不正确时无法发送表单?
  • 哇,谢谢。我还在学习角度。甚至不知道它的存在。现在我愿意。 tyvm :)
猜你喜欢
  • 2011-06-22
  • 2019-11-04
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
相关资源
最近更新 更多