好的,我有一个带有"是"和"否"单选按钮的表单。当用户单击任一时,它会触发一个 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,
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>
你好雅各布,看来你需要了解一些基础知识,以创建一个基础良好且整洁的Java脚本代码。我认为你的代码的问题在于你正在制造一些有时看不到的小问题。这是一个更新的代码,告诉我这是否有效。
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");
}
}
});
}
}
希望这有帮助!
<%@ page language="C#" AutoEventWireup="true" CodeFile="dentistJquery.aspx.cs" Inherits="dentistJquery" %><头>Jquery Validation with RegularExpressions. 头>