jquery validate plug and ignore (JavaScript and jquery newbie)
Is there a way to get the var ignoreClasses
into .validate()
?
$(document).ready(function(){
var ignoreClasses="";
//this hides or shows the specific sections base ond the relationship select
$("#relationship").change(function() {
switch ($(this).val()){
case 'prospective-customer':
$(".interest_all").hide();
$("#interest_1").show();
ignoreClasses = '.s2,.s4,.s5';
break;
case 'current-cutomer':
$(".interest_all").hide();
$("#interest_2").show();
ignoreClasses = '.s1,.s4,.s5';
break;
case 'prospective-partner':
$(".interest_开发者_JAVA百科all").hide();
$("#interest_3").show();
break;
case 'applicant':
$(".interest_all").hide();
$("#interest_4").show();
ignoreClasses = '.s1,.s2,.s5';
break;
case 'vendor':
$(".interest_all").hide();
$("#interest_5").show();
ignoreClasses = '.s1,.s2,.s4';
break;
default:
$(".interest_all").hide();
}
});
// field validation
$("#leadform").validate({
//I need to get the ignore class in here
//like ignore: '.s2,.s4,.s5'
});
});
Additionally, the entire form validation only works if the user changes the #Relationship
select box. If a user just completes a their name, then clicks submit the form is not validated at all.
$("#leadform").validate({ ignore: ignoreClasses});
You're passing in an object to the validate method, so you have to use JSON syntax.
Also, I suggest you cache your jQuery selectors for better performance (not that it'd matter much in this case, but it's a good practice).
http://jsfiddle.net/jondum/Tua6c/
if you want .validation()
to run with the ignoreClasses
you've specified within your change
event listener, you have to call the .validation()
method from within the event listener. As it is, ignoreClasses
will not be defined when .validate()
runs.
$(document).ready(function() {
var ignoreClasses = "";
var $interests_all = $(".interest_all");
//this hides or shows the specific sections base ond the relationship select
$("#relationship").change(function() {
switch (this.value) {
case 'prospective-customer':
$interests_all.hide();
$("#interest_1").show();
ignoreClasses = '.s2,.s4,.s5';
break;
case 'current-cutomer':
$interests_all.hide();
$("#interest_2").show();
ignoreClasses = '.s1,.s4,.s5';
break;
case 'prospective-partner':
$interests_all.hide();
$("#interest_3").show();
break;
case 'applicant':
$interests_all.hide();
$("#interest_4").show();
ignoreClasses = '.s1,.s2,.s5';
break;
case 'vendor':
$interests_all.hide();
$("#interest_5").show();
ignoreClasses = '.s1,.s2,.s4';
break;
default:
$interests_all.hide();
}
// field validation (now being called from within the change listener)
$("#leadform").validate({
ignore: ignoreClasses
});
});
});
精彩评论