我在灯箱中有两种形式(使用Magnific Popup)。默认情况下,jQuery验证是在显示时向第一个表单添加验证,但它不适用于任何其他表单,因为它们被Lightbox插件设置为display: none;
。
我需要它以便在打开的表单上删除验证,如果切换其可见性,将在第二个表单上运行。我在JS Fiddle中设置了它,看它目前是如何工作的。
答案 0 :(得分:1)
您需要做几件事。
最终代码:仅限JS
jq = jQuery;
jq(document).ready(function() {
runValidation();
// Lightboxes
jq('.mpf-open').magnificPopup({
type: 'inline',
mainClass: 'mfp-with-fade',
removalDelay: 300,
closeBtnInside: true,
closeOnBgClick: false,
callbacks: {
open: function() {
if(document.objValidSignIn) {
document.objValidSignIn.resetForm();
}
if(document.objValidSignUp) {
document.objValidSignUp.resetForm();
}
},
close: function() {
if(document.objValidSignIn) {
document.objValidSignIn.resetForm();
}
if(document.objValidSignUp) {
document.objValidSignUp.resetForm();
}
},
change: function() {
if(document.objValidSignIn) {
document.objValidSignIn.resetForm();
}
if(document.objValidSignUp) {
document.objValidSignUp.resetForm();
}
}
}
});
jq('form').submit(function(e) {
e.preventDefault();
});
});
jq.validator.setDefaults({
ignore: [],
// any other default options and/or rules
});
document.objValidSignIn = false, document.objValidSignUp = false;
// Validation
function runValidation() {
document.objValidSignIn = jq('#SignInForm').validate({
onsubmit: true,
focusInvalid: false,
rules: {
// Sign In
signin_email: {
required: true,
email: true
},
signin_password: {
required: true
},
signin_terms: {
required: true
},
signin_guidelines: {
required: true
}
},
highlight: function(element) {
jq(element).closest('.required').removeClass('valid').addClass('error');
},
success: function(element) {
jq(element).text('OK!').addClass('valid').closest('.required').removeClass('error').addClass('valid');
}
});
document.objValidSignUp = jq('#SignUpForm').validate({
onsubmit: true,
focusInvalid: false,
rules: {
// Sign Up
signup_name: {
required: true,
},
signup_screenname: {
required: true,
},
signup_email: {
required: true,
email: true
},
signup_passwordonce: {
required: true,
minlength: 8
},
signup_passwordtwice: {
required: true,
minlength: 8,
equalTo: '#passwordonce'
},
signup_terms: {
required: true
},
signup_guidelines: {
required: true
}
},
highlight: function(element) {
jq(element).closest('.required').removeClass('valid').addClass('error');
},
success: function(element) {
jq(element).text('OK!').addClass('valid').closest('.required').removeClass('error').addClass('valid');
}
});
}
演示:不使用jsfiddle可能是因为全局变量。它在jsbin环境中运行良好。