如何使用JS清理联系人表单后删除HTML5表单验证

How remove HTML5 form validation after cleaning contact form with JS

本文关键字:表单 删除 HTML5 验证 联系人 何使用 JS      更新时间:2023-09-26

我有一个联系表单,它在javascript中进行了验证。我想在提交邮件后清除表单。

用清洁

$(':input', '#contact')
.removeAttr('checked')
.removeAttr('selected')
.not(':button, :submit, :reset, :hidden, :radio, :checkbox')
.val('');

之后,表单是干净的,但 HTML5 验证被三重奏,所以我的输入以红色突出显示。如何去除红色,请帮忙。

(后来补充)我不知道该怎么办,我有他的代码:

$.ajax({
type: 'POST',
url: '/wordpress/wp-content/themes/conexion/config.php',
data: $("#contact").serialize()+"&filename="+response.filename+"&filepath="+response.filepath,
enctype: 'multipart/form-data',
success: function(data) {
if(data == "true") {
$("#message").replaceWith('<div id="message"><p>El mensaje</p><p>ha sido enviado</p></div>');                                       $(':input', '#contact')
.removeAttr('checked')
.removeAttr('selected')
.not(':button, :submit, :reset, :hidden, :radio, :checkbox')
.val('');
                                        }
                                    }
                                });
return false;

如果您仍然触发了 HTML5 验证,只需在表单标签中添加 novalidate 即可:

<form method="post" action="/foo" novalidate>...</form>

谢谢Alexalu和Ricky Stam,我终于禁用了HTML5验证,但没有在表单标签内。我在 jquery 中通过 $('#contact').attr('novalidate',true);因此,如果浏览器禁用了JS,它将使用HTML5进行验证。

<input type="button" value="Submit" id="btnsubmit" onclick="submitForm()">
function submitForm() {
   // Get the first form with the name
   // Hopefully there is only one, but there are more, select the correct index
   var frm = document.getElementsByName('contact-form')[0];
   frm.submit(); // Submit
   frm.reset();  // Reset
   return false; // Prevent page refresh
}