【问题标题】:Validate a form before it goes to ng-submit in Angular在表单进入 Angular 中的 ng-submit 之前验证表单
【发布时间】:2015-05-04 06:32:23
【问题描述】:

我有一个这样包装的表格:

<form ng-submit="processForm()">
...
        <div class="btn-group" role="group" ng-hide=0>
          <button type="button" value="dog" class="btn btn-default" ng-click="formData.type='dog' ">Dog</button>
          <button type="button" value="cat" class="btn btn-default" ng-click="formData.type='cat' ">Cat</button>
        </div>
...

所以我要做的一件事是在提交表单之前进行快速检查,以确保该人已单击这 2 个按钮之一。我可以将此代码粘贴到我的 processForm 代码中,但我觉得能够在 html 文件中进行快速验证并让 processForm 不处理视觉显示此错误之类的东西会更好。有什么办法还是我错了?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    我建议为此使用单选按钮。 还有一个由角度给出的误差变量。 通过混合这些东西和 html5,您可以简单地为此创建良好的验证。无需将其提交给函数。

        <form ng-submit="processForm()" name="form">
        ...
        <div class="input-group">
          <span class="input-group-addon">
              <input type="radio" name="type" ng-model="type" value="dog" required>  Dog <br/>
          </span>
          <span class="input-group-addon">
              <input type="radio" name="type" ng-model="type" value="cat" required>  Cat <br/>
          </span>
        </div>
        <div ng-show="form.name.$error.required">
        Please select one.
        </div>
    

    Here 就是这样。

    【讨论】:

      猜你喜欢
      • 2016-10-27
      • 2015-10-12
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 2012-03-29
      • 1970-01-01
      相关资源
      最近更新 更多