本文介绍了阻止表单提交,但保留表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何保留默认的HTML表单验证,但又禁止提交表单?我尝试使用event.preventDefault()return false.两者均成功阻止了表单的提交,但均未通过表单验证.我也尝试过此解决方案,但即使这样也不起作用.

How do I retain the default HTML form validation and yet prohibit form submitting? I have tried using event.preventDefault() and return false. Both are successful in stopping the form from submitting but both fail in form validation. I have also tried this solution, but even that does not work.

有人可以帮助我吗?

推荐答案

event.preventDefault()return false;都可以在表单提交中使用. HTML 5验证仍然存在.

Both event.preventDefault() and return false; work if used on form submit. The HTML 5 validation is still present.

jQuery("form").submit(function(e){
  e.preventDefault();
  //or
  //return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post">
  <input id="name" type="text" required="required" />
  <input id="email" type="email" required="required" />
  <input type="submit" id="btnSubmit" />
</form>

这篇关于阻止表单提交,但保留表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:00