我知道jQuery Validate默认只显示一个错误。我的目的是创建一个表单,只有在表单填写正确时才启用提交按钮。这是我的两个片段:
jQuery(document).ready(function() {
jQuery('input').on('blur', function() {
if (jQuery("#validate-info").valid()) {
jQuery('#toggle-delivery').removeClass("btn-disabled");
} else {
jQuery('#toggle-delivery').addClass("btn-disabled");
}
});
jQuery("#validate-info").validate({
rules: {
phone_number: {
required: true,
digits: true,
minlength: 9
},
email_address: {
required: true,
email: true
},
shipping_first_name: {
required: true,
minlength: 2
},
shipping_last_name: {
required: true,
minlength: 2
}
},
onfocusout: function(e) {
this.element(e);
},
onkeyup: false,
messages: {
phone_number: {
required: "Please enter a valid UK phone number",
digits: "Please enter a valid UK phone number.",
minlength: "Please enter at least 9 characters"
},
email_address: {
required: "We need your email address to contact you.",
email: "Oops! This isn't a correct email format. Please check and try again."
},
shipping_first_name: {
minlength: "Please enter at least 2 characters."
},
shipping_last_name: {
minlength: "Please enter at least 2 characters."
}
}
});
});
该按钮具有" btn-disabled"默认为class。如果表单有效,则删除该类,并且可以单击该按钮/提交表单。
如果用户离开页面而未提交表单,我也会使用此代码段来保留输入值:
jQuery(document).ready(function() {
jQuery(window).unload(saveSettings);
loadSettings();
});
function loadSettings() {
jQuery('#shipping_first_name.input-text').val(localStorage.shippingfirstname);
jQuery('#shipping_last_name.input-text').val(localStorage.shippinglastname);
jQuery('#email_address.input-text').val(localStorage.emailaddress);
jQuery("#phone_number.input-text").val(localStorage.phonenumber);
jQuery("#shipping_address_1.input-text").val(localStorage.shippingaddress);
jQuery('#billing_first_name.input-text').val(localStorage.billingfirstname);
jQuery('#billing_last_name.input-text').val(localStorage.billinglastname);
jQuery('#billing_email.input-text').val(localStorage.billingemailaddress);
jQuery("#billing_phone.input-text").val(localStorage.billingphonenumber);
jQuery("#billing_address_1.input-text").val(localStorage.billingaddress);
jQuery('input#ship-to-different-address-checkbox[value="' + localStorage.billtoaddress + '"]').prop('checked', true);
}
function saveSettings() {
localStorage.shippingfirstname = jQuery('#shipping_first_name.input-text').val();
localStorage.shippinglastname = jQuery('#shipping_last_name.input-text').val();
localStorage.emailaddress = jQuery('#email_address.input-text').val();
localStorage.phonenumber = jQuery("#phone_number.input-text").val();
localStorage.shippingaddress = jQuery("#shipping_address_1.input-text").val();
localStorage.billingfirstname = jQuery('#billing_first_name.input-text').val();
localStorage.billinglastname = jQuery('#billing_last_name.input-text').val();
localStorage.billingemailaddress = jQuery('#billing_email.input-text').val();
localStorage.billingphonenumber = jQuery("#billing_phone.input-text").val();
localStorage.billingaddress = jQuery("#billing_address_1.input-text").val();
localStorage.billtoaddress = jQuery('input#ship-to-different-address-checkbox[type=checkbox]:checked').val();
}
当用户返回页面时(通过意外刷新或导航到其他页面),这会自动填充表单。
我的问题是,现在,如果用户导航到页面以填写表单(第一次),并完成第一个字段,则在选择下一个字段时,将触发验证并显示所有其他字段红色,每个输入字段下都有错误消息。查看截图:
我该怎样防止这种情况?我想验证每个字段并单独显示错误,而不是一次只显示错误。我怀疑它与代码片段的on('blur'
部分有关。我尝试了change
和keyup
而不是blur
,但结果是一样的。当然,我不是专家,所以我不确定这是不是真正的问题。
任何提示?谢谢!
答案 0 :(得分:1)
我同意它很可能是由您的blur
事件处理程序引起的。尝试更改此内容:
if (jQuery("#validate-info").valid()) {
对此:
if (jQuery("#validate-info").validate().checkForm()) {
checkForm()
函数应根据表单是否有效返回布尔值,但不会导致出现任何验证错误消息。