我对 JQuery Validate 和自定义方法有一个奇怪的错误。出于某种原因,每次更改字段时,验证方法都会触发 3-4 次。它出于某种原因循环,我不知道为什么。验证器工作。唯一的问题是它不能与 datepicker 一起使用。我必须在日期选择器中单击两次才能更改输入中的值。似乎 jquery validate 正在劫持 datepicker 输入或其他东西。在这一点上我一无所知,并且会接受任何想法。
自定义方法:
$(document).ready(function () {
$.validator.addMethod("laterDate", function(value, element) {
var id = "#" + element.id;
var id_number = element.value.charAt(element.length - 1);
alert(id_number);
if(id_number == "1"){
alert("in if")
var id_value = $(id).fieldDate();
var today = new Date();
alert(id_value > today);
return id_value > today;
}
else{
alert("in else")
var prev_id_number = String(parseInt(id_number) - 1);
var prev_id = "#end_date_" + prev_id_number;
var prev_id_value = $(prev_id).fieldDate();
var id_value = $(id).fieldDate();
return id_value > prev_id_value;
}
}, $.format("Enter a later date"));
});
验证器代码是:
$(document).ready(function () {
$("#rate_new_new").validate({
debug: true,
rules: {
"rates[start_date_1]": {dateITA: true, maxlength: 50},
"rates[end_date_1]": {dateITA: true, maxlength: 50, laterDate: true},
"rates[price_1]": {testPrice: true}
},
messages: {
"rates[start_date_1]": {
dateITA: "Please enter a valid date",
maxlength: "The name must be less than 50 characters"
}
},
submitHandler: function(form) {
$(form).ajaxSubmit({
success: function(){
$('#lefttabs li:eq(4) a').tab('show');
}
});
}
});
});