jquery验证插件并忽略(JavaScript和jquery新手)

jquery验证插件并忽略(JavaScript和jquery新手),jquery,validation,ignore,Jquery,Validation,Ignore,我承认我对jQuery甚至bacicJS都不是很在行,但是我已经通过一个非常酷的表单进行了黑客攻击,但是我需要有人帮助我解决最后一个问题 $(document).ready(function(){ var ignoreClasses=""; //this hides or shows the specific sections base ond the relationship select $("#relationship").change(function() { switch

我承认我对jQuery甚至bacicJS都不是很在行,但是我已经通过一个非常酷的表单进行了黑客攻击,但是我需要有人帮助我解决最后一个问题

$(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_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'


});
})

是否有方法将var ignoreClasses放入到.validate中

乔纳森


非常感谢您将这些值输入到忽略中。但有一个问题。仅当用户更改关系选择框时,整个表单验证才有效。如果用户只是填写了他们的姓名,则单击“提交”,该表单根本不会被验证。

$leadform.validate{ignore:ignoreClasses}

您正在向传递对象,因此必须使用JSON语法

此外,我建议您缓存jQuery选择器以获得更好的性能,这在本例中并不重要,但这是一种很好的做法

如果希望.validation与在更改事件侦听器中指定的IgnoreClass一起运行,则必须从事件侦听器中调用.validation方法。实际上,当.validate运行时,将不会定义IgnoreClass


将var放入.validate是什么意思?非常感谢您将值放入ignore。但有一个问题。仅当用户更改关系选择框时,整个表单验证才有效。如果用户只是填写了一个他们的名字,然后单击“提交”,则表单根本不会被验证。然后,您需要做的是在$document.ready之外声明ignoreClasses,以便它成为一个全局变量。然后使用提交侦听器将验证函数附加到表单提交:$'formleadform'。提交函数{$this.validate{ignore:ignoreClasses};};您还可以将ignoreClasses保留在原来的位置,并像window['ignoreClasses']=那样声明它。然后,您可以在提交处理程序中以同样的方式引用它。非常感谢Jonathan,但我在这里太不知所措了,所以我不知道是否可以让您的解决方案起作用。我将在上面发布我的最新代码。不要泄气!你快到了。
$(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
        });
    });
});