如何解决jQuery验证resetForm函数不起作用
| 好的,我有一个具有“是”和“否”单选按钮的表单。当用户单击其中一个时,它会触发radioChanged()函数,该函数检查以查看选中了哪个框。如果未选中,它将显示电子邮件字段并根据规则验证表单。如果用户单击“是”,我需要重置并清除验证,但它似乎无法正常工作。如果选中不需要电子邮件字段的新验证,是否应该重新验证?<script type=\"text/javascript\">
var validator = $(\"#newClient\");
function radioChange() {
if (document.getElementById(\"yesbutton\").checked == true) {
document.getElementById(\"emailSpan\").style.display = \"none\";
document.getElementById(\"cemailSpan\").style.display = \"none\";
document.getElementById(\"emailError\").style.display = \"none\";
document.getElementById(\"cemailError\").style.display = \"none\";
validator.resetForm();
} else if (document.getElementById(\"nobutton\").checked == true) {
document.getElementById(\"emailSpan\").style.display = \'block\';
document.getElementById(\"cemailSpan\").style.display = \'block\';
document.getElementById(\"emailError\").style.display = \"block\";
document.getElementById(\"cemailError\").style.display = \"block\";
validator.validate({
rules: {
Email: {
required: true,minlength: 4,maxlength: 48,email: true
},ConfirmEmail: {
required: true,email: true,equalTo: \"#Email\"
}
},messages: {
Email: {
required: \"Please enter a valid email address\",email: \"Please enter a valid email address\",maxlength: \"Max length is 48\"
},ConfirmEmail: {
required: \"Please enter a valid email address\",maxlength: \"Max length is 48\",equalTo: \"Emails do not match\"
}
},errorPlacement: function(error,element) {
if (element.attr(\"name\") == \"ConfirmEmail\") error.appendTo(\"#cemailError\");
else if (element.attr(\"name\") == \"Email\") error.appendTo(\"#emailError\");
}
})
}
}
</script>
解决方法
您好雅各布,您似乎需要了解一些基本知识,以创建井井有条的Java脚本代码。我认为您的代码存在的问题是您遇到了一些小问题,有时无法看到。这是更新的代码,请告诉我这是否有效。
带有正则表达式的jQuery验证。 title>
var validator = $(\"#newClient\");
function radioChange() {
if (document.getElementById(\"yesbutton\").checked === true) {
document.getElementById(\"emailSpan\").style.display = \"none\";
document.getElementById(\"cemailSpan\").style.display = \"none\";
document.getElementById(\"emailError\").style.display = \"none\";
document.getElementById(\"cemailError\").style.display = \"none\";
validator.resetForm();
} else if (document.getElementById(\"nobutton\").checked === true) {
document.getElementById(\"emailSpan\").style.display = \'block\';
document.getElementById(\"cemailSpan\").style.display = \'block\';
document.getElementById(\"emailError\").style.display = \"block\";
document.getElementById(\"cemailError\").style.display = \"block\";
validator.validate({
rules: {
Email: {
required: true,minlength: 4,maxlength: 48,email: true
},ConfirmEmail: {
required: true,email: true,equalTo: \"#Email\"
}
},messages: {
Email: {
required: \"Please enter a valid email address\",email: \"Please enter a valid email address\",maxlength: \"Max length is 48\"
},ConfirmEmail: {
required: \"Please enter a valid email address\",maxlength: \"Max length is 48\",equalTo: \"Emails do not match\"
}
},errorPlacement: function(error,element) {
if (element.attr(\"name\") === \"ConfirmEmail\") {
error.appendTo(\"#cemailError\");
}
else if (element.attr(\"name\") === \"Email\") {
error.appendTo(\"#emailError\");
}
}
});
}
}
希望这可以帮助!
,<%@页面语言= \“ C#\” AutoEventWireup = \“ true \” CodeFile = \“ dentistJquery.aspx.cs \” Inherits = \“ dentistJquery \”%>
<!DOCTYPE html PUBLIC \“-// W3C // DTD XHTML 1.0 Transitional // EN \” \“ http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd \”>
<头>