我有以下 JavaScript 函数:
$('div.nextStepBilling').click(function (e) {
if ($(this).parent().parent().attr("id") == "newCardDiv") {
$('form#myForm').validate({
rules: {
cardHolder: "required",
cardNumber: {
required: true,
minlength: 15,
maxlength: 16
},
CVV: {
required: true,
minlength: 3,
maxlength: 4
}
},
messages: {
cardHolder: "Card holder name is required",
cardNumber: {
required: "Credit card number is required",
minlength: "Credit card must be at least 15 digits long",
maxlength: "Credit card must be at most 16 digits long"
},
CVV: {
required: "CVV (security code) is required",
minlength: "CVV must be at least 3 digits long",
maxlength: "CVV must be at most 4 digits long"
}
}
});
if ($('form#myForm').valid()) {
alert("valid");
} else {
alert("invalid");
}
}
});
当我单击正确div.nextStepBilling
时,即使这些输入中没有任何内容,它总是会发出有效警报。
我在控制台中没有收到任何错误,我可以让 jquery validate 使用$('form#myForm').validate().element("#cardHolder");
我在页面上包含最新的 jquery 和 jquery validate 包(以正确的顺序)。任何人都可以看到上面的代码有什么问题吗?