以下是如何根据开始或结束日期的选择禁用日期:
如果在开始日期框中选择了日期,则结束日期将仅限于选择开始日期之后的天数(包括开始日期),并且结束日期也将被限制。
$(document).on('pageinit', '#index', function(){
$('#end-date').bind('datebox', function(e, p) {
if ( p.method === 'open') {
// Make it a date
var startDate = new Date($('#start-date').val());
var todaysDate = new Date();
// Length of 1 Day
var lengthOfDay = 24 * 60 * 60 * 1000;
// Get the difference
var diff = parseInt((((startDate.getTime() - todaysDate.getTime()) / lengthOfDay)+1)*-1,10);
// Set minDays to disallow anything earlier
$('#end-date').datebox({'minDays': diff});
}
});
$('#start-date').bind('datebox', function(e, p) {
if ( p.method === 'open') {
// Make it a date
var endDate = new Date($('#end-date').val());
var todaysDate = new Date();
// Length of 1 Day
var lengthOfDay = 1000 * 60 * 60 * 24;
// Get the difference
var diff = parseInt((((endDate.getTime() - todaysDate.getTime()) / lengthOfDay)+1),10);
// Set minDays to disallow anything earlier
$('#start-date').datebox({'maxDays': diff});
}
});
});
检查更新的小提琴链接:http: //jsfiddle.net/QTuma/6/
或者按照下面的解决方案,一种更简单的方法来设置最小值和最大值:
$(document).on('pageinit', '#index', function(){
$('#start-date').bind('datebox', function(e, p) {
if ( p.method === 'set') {
var endDate = $('#start-date').val().replace(/(\d{1,2})\/(\d{1,2})\/(\d{4})$/,'$3-$1-$2');
$('#end-date').attr({'min':endDate,'max':'2999-01-01'});
$('#end-date').datebox('applyMinMax');
}
});
$('#end-date').bind('datebox', function(e, p) {
if ( p.method === 'set') {
var endDate = $('#end-date').val().replace(/(\d{1,2})\/(\d{1,2})\/(\d{4})$/,'$3-$1-$2');
$('#start-date').attr({'min':'1000-01-01','max':endDate});
$('#start-date').datebox('applyMinMax');
}
});
});