简体   繁体   English

欧芹:按下提交以外的按钮时如何防止验证?

[英]Parsley: How to prevent validation when pressing buttons other than submit?

Thank you for any help.感谢您的任何帮助。

I am trying to use Parsley for Form Validation.我正在尝试使用 Parsley 进行表单验证。 My form has one submit button and some other buttons to dynamically add inputs to the form.我的表单有一个提交按钮和一些其他按钮,用于向表单动态添加输入。 But when I press these other buttons, form validation is carried out.但是当我按下这些其他按钮时,就会执行表单验证。 But I am not submitting any form.但我没有提交任何表格。

How to prevent form validation from happening when I press other buttons than submit button?当我按下提交按钮以外的其他按钮时,如何防止表单验证发生?

Sorry, I dont know how to JS Fiddle.抱歉,我不知道如何使用 JS Fiddle。 My code is like the following:我的代码如下:

<form method="post" action="confirm" data-parsley-validate>
    <input id="brand" data-parsley-trigger="submit" required />
    <button id="addQuantity">Add</button>
    <input type="number" required data-parsley-trigger="submit" />
    <input type="submit" value="Submit">
</form>

When I press Add, the form is validated.当我按下添加时,表单被验证。 How should I prevent this?我应该如何防止这种情况? Thank you very much.非常感谢你。

The tag button which was introduced in HTML5 is equivalent to input type="submit" hence when you press add it will automatically fire submit action. HTML5中引入的标记按钮等效于输入type =“ submit”,因此当您按下添加按钮时,它将自动触发提交操作。 What you can do is replace the tag to input type="button" or you can prevent the default action in jquery like this 您可以做的就是替换标签以输入type =“ button”,或者您可以在jquery中阻止默认操作,例如:

 <script> 
   $('#addQuantity').click(function(event)
    {
       event.preventDefault();
       //do your action goes below
    });
 </script>

I found adding formnovalidate to the button skipped form validation for the form only when clicking that button.我发现将formnovalidate添加到按钮时,只有在单击该按钮时才会跳过表单的表单验证。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM