【问题标题】:AngularJS Form Validation and SubmitAngularJS 表单验证和提交
【发布时间】:2017-05-10 14:09:20
【问题描述】:

Codepen 链接:http://codepen.io/anon/pen/mVyPaw

需要创建一个 Angular 应用程序(带 1 个控制器)来验证此表单是否已填写所有三个字段,并且只有在表单有效时才能启用提交按钮。不需要使用预处理器。

<div class="container" ng-app="myApp" ng-controller="formCtrl">

  <h1><span><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/15309/angular-logo.svg" alt="Angular.js Logo" /></span> Angular.js - Web Form</h1>

  <form role="form" name="form" id="contact-form">
    <div class="form-group">
      <label for="FirstName">First name</label>
      <input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="enter first name" ng-model="firstName">
    </div>

    <div class="form-group">
      <label for="LastName">Last Name</label>
      <input type="text" class="form-control" name="LastName" id="LastName" placeholder="enter last name" ng-model="lastName">
    </div>

    <div class="form-group">
      <label for="EmailAddress">Email address</label>
      <input type="email" class="form-control" id="EmailAddress" name="EmailAddress" placeholder="enter email" ng-model="emailAddress" ng-model-options="{ updateOn: 'blur' }">
    </div>

    <div class="checkbox">
      <label>
        <input type="checkbox" required> Check me out
      </label>
    </div>

    <button type="submit" class="btn btn-default" ng-model="submitButton">Submit</button>

  </form>

</div>

【问题讨论】:

  • 您尝试使用验证吗?
  • 查看我的更新答案
  • 我对 Angular 有点陌生,所以不知道该怎么做

标签: angularjs


【解决方案1】:

其实是模块注入问题。 ng-app="myApp" ng-controller="formCtrl" 你的 myApp 模块代码在哪里。

像这样试试。

<div class="container" ng-app>

  <h1><span><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/15309/angular-logo.svg" alt="Angular.js Logo" /></span> Angular.js - Web Form</h1>

  <form role="form" name="form" id="contact-form" novalidate>
    <div class="form-group">
      <label for="FirstName">First name</label>
      <input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="enter first name" ng-model="firstName" required>
    </div>

    <div class="form-group">
      <label for="LastName">Last Name</label>
      <input type="text" class="form-control" name="LastName" id="LastName" placeholder="enter last name" ng-model="lastName" required>
    </div>

    <div class="form-group">
      <label for="EmailAddress">Email address</label>
      <input type="email" class="form-control" id="EmailAddress" name="EmailAddress" placeholder="enter email" ng-model="emailAddress" ng-model-options="{ updateOn: 'blur' }" required>
    </div>

    <div class="checkbox">
      <label>
        <input type="checkbox" required> Check me out
      </label>
    </div>


    <button type="submit" class="btn btn-default" ng-disabled="form.$invalid">Submit</button>

  </form>

</div>

【讨论】:

  • 假设 Angular 库已经包含在这个 codepen 中(1.3.0)
  • 如果您还想拥有引导程序增强的错误消息怎么办?这似乎摆脱了那个
【解决方案2】:

添加所需的验证

 <div class="container" ng-app="myApp" ng-controller="formCtrl">

  <h1><span><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/15309/angular-logo.svg" alt="Angular.js Logo" /></span> Angular.js - Web Form</h1>

  <form role="form" name="myForm" id="contact-form">
    <div class="form-group">
      <label for="FirstName">First name</label>
      <input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="enter first name" ng-model="firstName" required>
    </div>

    <div class="form-group">
      <label for="LastName">Last Name</label>
      <input type="text" class="form-control" name="LastName" id="LastName" placeholder="enter last name" ng-model="lastName" required >
    </div>

    <div class="form-group">
      <label for="EmailAddress">Email address</label>
      <input type="email" class="form-control" id="EmailAddress" name="EmailAddress" placeholder="enter email" ng-model="emailAddress" ng-model-options="{ updateOn: 'blur' }" required>
    </div>

    <div class="checkbox">
      <label>
        <input type="checkbox" required> Check me out
      </label>
    </div>

    <button type="submit" class="btn btn-default" ng-model="submitButton" data-ng-disabled="myForm.$invalid" >Submit</button>

  </form>

</div>

这是您编辑的CodePen

【讨论】:

  • 谢谢大家!为什么@ManikandanVelayutham 错了?似乎对我有用
  • 按F12就知道了。
  • 你好按钮不应该点击
猜你喜欢
  • 1970-01-01
  • 2016-05-19
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 2021-09-08
  • 2018-06-12
相关资源
最近更新 更多