好吧我有一个窗体有,没有单选按钮。当用户点击其中一个时,它会触发一个radioChanged()函数,该函数检查哪个框被检查。如果选中“否”,则会显示电子邮件字段并根据规则验证表单。如果用户点击“是”,我需要验证重置并清除,但似乎没有工作。如果选中“是”而不需要电子邮件字段,我是否应该进行新的验证?jquery验证resetForm()函数不起作用
<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,
minlength: 4,
maxlength: 48,
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",
email: "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>
我觉得你是在49行15字符缺少分号,请检查一下,并用''===来比较它比较快,并以安全的方式。并且请使用'if(// * Condition * //){// *在大括号内做一些事情!* //}'这让浏览器更好地计算'if/else'。 – 2011-05-24 04:33:16
那么没有工作,但我发现我的答案在这里,它完美的作品!http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression – Throttlehead 2011-05-24 04:46:51
是的,我知道,但我只是提供给你一些提示,使您的代码看起来不错,而不是杂乱。 – 2011-05-24 08:04:44