gpt4 book ai didi

forms - ng-submit 不会阻止提交

转载 作者:行者123 更新时间:2023-12-04 23:21:36 24 4
gpt4 key购买 nike

SO上有很多类似的问题。我已经经历了很多,但没有发现我的问题。

我有一个使用 ng-submit 的表单.不幸的是,即使表单无效,按 Enter 或单击提交也能工作。 ng-submit方法被调用。

CodePen 示例:http://codepen.io/calendee/pen/tgFhq

<form name="testForm" novalidate ng-submit="submit()">

<label for="firstname">First Name:</label>
<input type="text" name="firstname" ng-model="data.firstname" required="true" ng-minlength="4" ng-maxlength="10">

<div class="errors">
<p>firstname Errors:</p>
<p ng-show="testForm.firstname.$error.required">firstname is required</p>
<p ng-show="testForm.firstname.$error.minlength">firstname is too short</p>
<p ng-show="testForm.firstname.$error.maxlength">firstname is too long</p>
</div>

<label for="lastname">Last Name:</label>
<input type="text" name="lastname" ng-model="data.lastname" ng-required="true" ng-minlength="4" ng-maxlength="10">

<div class="errors">
<p>lastname Errors:</p>
<p ng-show="testForm.lastname.$error.required">lastname is required</p>
<p ng-show="testForm.lastname.$error.minlength">lastname is too short</p>
<p ng-show="testForm.lastname.$error.maxlength">lastname is too long</p>
</div>


<button class="button button-balanced" type="submit">Submit</button>

</form>

如果我更改表单 ng-form即使表单有效,提交事件也根本不起作用。

CodePen 示例: http://codepen.io/calendee/pen/imJdc
<ng-form name="testForm" novalidate ng-submit="submit()">

<label for="firstname">First Name:</label>
<input type="text" name="firstname" ng-model="data.firstname" required="true" ng-minlength="4" ng-maxlength="10">

<div class="errors">
<p>firstname Errors:</p>
<p ng-show="testForm.firstname.$error.required">firstname is required</p>
<p ng-show="testForm.firstname.$error.minlength">firstname is too short</p>
<p ng-show="testForm.firstname.$error.maxlength">firstname is too long</p>
</div>

<label for="lastname">Last Name:</label>
<input type="text" name="lastname" ng-model="data.lastname" ng-required="true" ng-minlength="4" ng-maxlength="10">

<div class="errors">
<p>lastname Errors:</p>
<p ng-show="testForm.lastname.$error.required">lastname is required</p>
<p ng-show="testForm.lastname.$error.minlength">lastname is too short</p>
<p ng-show="testForm.lastname.$error.maxlength">lastname is too long</p>
</div>


<button class="button button-balanced" type="submit">Submit</button>

</ng-form>

有人对我在这里做错了什么有什么建议吗?

最佳答案

您可以使用内置属性 $valid :

<form name="testForm" novalidate ng-submit="testForm.$valid && submit()">

提交函数仅在 testForm.$valid 时调用是真的。

我在本教程中学到了它: https://www.codeschool.com/courses/shaping-up-with-angular-js .

关于forms - ng-submit 不会阻止提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24528764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com