隨便用beforeShowDay
回調。對不起,我有一個recurrent_period中的錯誤,我花了一段時間才發現。我在測試套件中添加了「年度」案例。讓我知道是否有任何我在這裏看過的東西。有趣的問題! jsFiddle
var invalid = { "single": [ "2/4/2012", "3/2/2012" ], "recurrent_day": [ 0 ], "recurrent_date": [ 28 ], "period": [ { "from": "2/21/2012", "to": "2/22/2012" } ], "recurrent_period": [ { "from": "2/28/2012", "to": "2/29/2012", "period": "monthly" },{ "from": "2/7/2012", "to": "2/9/2012", "period": "yearly" } ] };
function single(date){
var USdate = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
return ($.inArray(USdate,invalid.single) > -1);
}
function recurrent_day(date){
return ($.inArray(date.getDay(),invalid.recurrent_day) > -1);
}
function recurrent_date(date){
return ($.inArray(date.getDate(),invalid.recurrent_date) > -1);
}
function period(date){
var i, num, period, start, startArray, end, endArray;
num = invalid.period.length;
for(i=0;i<num;i++){
period = invalid.period[i];
startArray = period.from.split('/');
start = new Date(startArray[2], (startArray[0] - 1), startArray[1]);
endArray = period.to.split('/');
end = new Date(endArray[2], (endArray[0] - 1), endArray[1]);
if(date>=start && date<=end){
return true;
}
}
return false;
}
function recurrent_period(date){
var i, num, period, recurrence, startArray, endArray, startDay, endDay, start, end;
num = invalid.recurrent_period.length;
for(i=0;i<num;i++){
period = invalid.recurrent_period[i];
recurrence = period.period;
startArray = period.from.split('/');
endArray = period.to.split('/');
if(recurrence === 'monthly'){
startDay = parseInt(startArray[1], 10);
endDay = parseInt(endArray[1], 10);
if(date.getDate() >= startDay && date.getDate() <= endDay){
return true;
}
}else if(recurrence === 'yearly'){
start = new Date(date.getFullYear(), (startArray[0] - 1), startArray[1]);
end = new Date(date.getFullYear(), (endArray[0] - 1), endArray[1]);
console.log({start:start.toDateString() ,end:end.toDateString(),day:date.toDateString()})
if(date>=start && date<=end){
return true;
}
}
}
return false;
}
$('input').datepicker({
beforeShowDay: function(date){
if(single(date)){
return [false];
}else if(recurrent_day(date)){
return [false];
}else if(recurrent_date(date)){
return [false];
}else if(period(date)){
return [false];
}else if(recurrent_period(date)){
return [false];
}
return [true];
}
});
如果jQuery提供一個乾淨的方式來做到這一點,那麼它是好的,否則我會建議任何其他日期選擇器,甚至定製。 – Shaheer 2012-02-20 08:28:01
潛在的「時期」是什麼?你已經使用「每月」,但這只是一個嚴重的日期,應該給予「recurrent_date」。你想每年?是否有另一個「每月」的理由? – Sinetheta 2012-02-20 08:38:30
@Sinetheta:是的你的正確。每年還有另一種選擇。 – Gowri 2012-02-20 08:45:46