简体   繁体   English

当未填写必填字段时,不调用角提交

[英]Angular submit not called when required fields not filled

I'm creating a form in Angular with Ionic. 我正在用Ionic在Angular中创建一个表单。 I don't want a red error class to be displayed unless a user has submitted the form. 我不希望显示红色错误类,除非用户提交了表单。 As such, my code looks like this: 因此,我的代码如下所示:

<form ng-submit="submit()" name="form">
     <ion-radio ng-repeat="item in items" ng-model="data.type" name="type" ng-value="item.value" ng-class="{'error' : form.type.$invalid && formSubmitted }"
</form>

And then in my controllers 然后在我的控制器中

$scope.submit = function ()
{
   $scope.formSubmitted = true; //Tell our errors they can show now
}

So the error class should only show up when formSubmitted is true. 因此,仅当formSubmitted为true时,错误类才应显示。 For whatever reason, however, required prevents submit from being called. 但是,无论出于何种原因,都required阻止submit被调用。 This isn't the case for other attributes such as minlength. 其他属性(例如minlength)则不是这种情况。

How can I get the behavior I want? 如何获得我想要的行为?

You need to add novalidate to your form. 您需要在表单中添加novalidate

The reason being, your browser is validating your form rather than letting your code do it. 原因是,您的浏览器正在验证您的表单,而不是让您的代码来执行它。

alternatively, do something like: 或者,执行以下操作:

<form ng-submit="submit()" name="form" novalidate>
     <ion-radio ng-repeat="item in items" ng-model="data.type" name="type" ng-value="item.value" ng-class="{'error' : form.type.$invalid && form.$submitted }"
</form>

form.$submitted will become true when you submit. form.$submitted将在您提交时变为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果未填写必填字段,如何防止提交表单上的函数调用? - How to prevent function call on submit form if required fields are not filled out? 在填写动态创建的必填字段之前,请禁用提交按钮 - Keeping submit button disabled until dynamically created required fields are filled 即使未填写必填字段,我是否可以在超时时提交表单 - Can I submit form on timeout even if required fields are not filled GTM - 表单提交时的Fire标记(仅当填写了必填字段时) - GTM - Fire tag on form submit (only if required fields are filled) 如何禁用提交,直到仅填写必填字段 - How to disable submit till only required fields are filled up HTML 必填字段在未填写时不显示消息 - HTML required fields are not showing a message when it's not filled out 填写所有必填字段时如何防止默认提交 - How to prevent default submission when ALL required fields are filled in 未填写必填字段时停止提交表单 - Stop form submission when the required fields have not been filled out 填写必填字段时,使用 Javascript 触发 animation - trigger animation using Javascript when required fields are filled 当一个字段被填充时,其他字段必须被要求 - when one field is filled other fields must be required
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM