2009-08-03 62 views
0
rules: { 
firstname: "required", 
lastname: "required", 
birthdate: { 
    date: true, 
    required: true, 
    minimumAge: true 
}, 
consent: { 
    required: function(element) { 
    return getAge($("#birthdate").val()) < 18; 
    $("#consent").show(); 
    } 
} 
}); 
$("#consent").hide(); 

我想顯示覆選框的同意如果用戶的年齡低於18,從上述代碼爲什麼不工作?隱藏和顯示某些字段與jQuery驗證

回答

1
return getAge($("#birthdate").val()) < 18; 

^你返回一個布爾值,而函數執行完畢,因此不會處理$(「#同意」)。節目()..你可能是指如果以這個包裹在聲明:

if (getAge($('#birthdate').val()) < 18) { 
    $('#consent').show(); 
} 

或逆轉<來>,但它的作品了。

+0

您的第一個答案將起作用,但是將< to >顛倒過來無效。它只會返回OP中代碼的反面。 – Marius 2009-08-03 02:09:23