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

时间:2011-09-17 00:14:59

标签: jquery validation ignore

让我承认我对jQuery甚至bacic JS都不是很了解,但是我已经通过一个非常酷的形式入侵了我的方式,但是我需要有人帮我解决我遇到的最后一个问题。

$(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()


乔纳森,

非常感谢,这有助于将值变为忽略。但有一个问题。仅当用户更改#Relationship选择框时,整个表单验证才有效。如果用户只是完成了他们的名字,那么点击提交表单根本不会被验证。

2 个答案:

答案 0 :(得分:0)

$("#leadform").validate({ ignore: ignoreClasses});

您将对象传递给validate method,因此您必须使用JSON语法。

另外,我建议你缓存你的jQuery选择器以获得更好的性能(在这种情况下并不重要,但这是一个很好的做法)。

http://jsfiddle.net/jondum/Tua6c/

答案 1 :(得分:0)

如果您希望.validation()ignoreClasses事件监听器中指定的change一起运行,则必须在事件监听器中调用.validation()方法。实际上,ignoreClasses运行时不会定义.validate()

$(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
        });
    });
});