0

我有一个联系表格,它在 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;
4

3 回答 3

1

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

<form method="post" action="/foo" novalidate>...</form>
于 2013-11-04T14:01:25.207 回答
0

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

于 2013-11-12T10:19:16.587 回答
-1
<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
}
于 2013-11-04T13:59:53.167 回答