简体   繁体   English

如何在点击按钮发送表单时关闭Angular中的表单验证

[英]How to turn off form validation in Angular when clicking by button that shoulden't send form

I have a form like this one: 我有一个这样的表格:

<form name="myForm"> 
    <input id="myName" ng-model="myName" required/>
    ...
    <button ng-click="doSomePrepare()">Do some prepare</button>
    ...
    <button ng-click="saveForm()" ng-disabled="myForm.$invalid">Save</button>
</form>

I want validate form only when "Save" button clicked. 我只想在单击“保存”按钮时验证表单。

But if I'm click "Do some prepare", browser(chrome) ask me to fill "myName" input. 但如果我点击“做一些准备”,浏览器(chrome)会要求我填写“myName”输入。

It's make me problems, cause in real world "Do some prepare" button may be a datepicker button or a dropdown or somethig like that. 它让我有问题,因为在现实世界中“做一些准备”按钮可能是一个datepicker按钮或下拉或类似的东西。

PS . PS I'm use Angular 1.2 version, but as far as I know this can be reprodused with version 1.0 我使用Angular 1.2版本,但据我所知,这可以用1.0版本重现

JSfiddle : http://jsfiddle.net/se_panfilov/wQF4S/4/ JSfiddlehttp//jsfiddle.net/se_panfilov/wQF4S/4/

Make your first button a non-form-submitting button: 将第一个按钮设为非表单提交按钮:

<button type="button" ng-click="doSomePrepare()">Do some prepare</button>

You can test it here 你可以在这里测试一下

The default type attribute of a button tag is submit . button标记的默认type属性是submit Try setting the type to reset or to button to prevent the submit event from firing when you click it. 尝试将type设置为resetbutton以防止单击时触发submit事件。

Working demo: http://jsfiddle.net/wQF4S/3/ 工作演示: http//jsfiddle.net/wQF4S/3/

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

相关问题 关闭已禁用的表单控件Angular的验证 - Turn Off Validation for Disabled Form Controls Angular Angular 7 单击提交按钮时表单验证不显示错误 - Angular 7 Form Validation does not show error when clicking submit button 单击“ Enter”按钮时提交表单,并进行验证 - Submit form when Clicking “Enter” button, with validation 切换反应形式时如何关闭表单的自动完成? - how to turn off autocomplete of a form when switching in react form? "如何从 parsley.js 表单验证中打开\/关闭字段" - How to turn on/off a field from parsley.js form validation 关闭某些字段的表单验证 - Turn off Form Validation for certain fields 单击提交按钮时,表单验证无法正常工作? - Form validation is not working properly when clicking submit button? 单击 Bootstrap 中的按钮时,打开带有验证的模态表单 - Open a modal form with validation when clicking on a button in Bootstrap 如何通过单击单选按钮将值发送到隐藏的表单域 - How to send values to hidden form field by clicking on radio button 单击我的Web表单上的Submit按钮时,即使这些字段具有值,输入字段的验证错误也会如何显示? - When clicking the Submit button on my web form, how come the validation errors for the Input fields display, even when these fields have values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM