我有 2 个输入框,开始日期和结束日期。我试图强制用户在第一个输入框中的日期之后的第二个输入框中选择一个日期。这将在 ajax 保存时得到验证。
包括 isvalid 在内的所有内容在 ie 中都很好用,但在 FF 或 Opera 中不起作用。在 FF 和 opera 中,无论开始日期之前或之后的日期是什么,它只是一直显示 isValid:“必须在开始日期之后”消息而不进行验证。
有没有人知道为什么,或者使用日期选择器的解决方案,所以它修改了第二个日期选择器上的 minDate。
以下是 ajax 模式的按钮:
buttons: {
'Save': function() {
if($("#new_request_form").valid()){
// do stuff if form validates
submitFormWithAjax($("#new_request_form"));
$(this).dialog('close');
} else {
// do stuff if form does not validate
}
},
'Cancel': function() {$(this).dialog('close');}
},
这是日期检查,以确保结束日期在开始日期之后:
jQuery.validator.addMethod("isValid", function (value, element) {
var startDate = $('#startdate').val();
var finDate = $('#enddate').val();
return Date.parse(startDate) < Date.parse(finDate);
}, "* End date must be after start date");
$("#new_request_form").validate({
rules: {
days: "required",
hours: {
required: true,
number: true,
digits: false
},
startdate: "required",
enddate: {
required: true,
isValid: true
}
},
messages:{
days: "Required",
hours: {
required: "Required",
number: "Numbers Only"
},
startdate: "Required",
enddate: {
required: "Required",
isValid: "Must be after start date"
}
}
});
var $ac_start_date = '<?php echo $ac_end_date ?>',
$ac_start_date_flip = '<?php echo $ac_end_date_flip ?>',
$ac_start_parsed = Date.parse($ac_start_date),
_today = new Date().getTime();
// For Opera and older winXP IE n such
if (isNaN($ac_start_parsed)) {
$ac_start_parsed = Date.parse($ac_start_date_flip);
}
var _aDayinMS = 1000 * 60 * 60 * 24;
// Calculate the difference in milliseconds
var difference_ms = Math.abs($ac_start_parsed - _today);
// Convert back to days and return
var DAY_DIFFERENCE = Math.round(difference_ms/_aDayinMS);
// do initialization here
$("#startdate").datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
yearRange: '0:+100',
minDate: '+1d',
maxDate: '+' + (DAY_DIFFERENCE + 1) + 'd'
});
// do initialization here
$("#enddate").datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
yearRange: '0:+100',
minDate: '+1d',
maxDate: '+' + (DAY_DIFFERENCE + 1) + 'd'
});
}, 'html');
return false;
});
}
function submitFormWithAjax(form) {
form = $(form);
$.ajax({
url: form.attr('action'),
data: form.serialize(),
type: (form.attr('method')),
dataType: 'script',
success: function(data){
}
});
return false;
}