試試這個
$(function() {
var startDate;
var endDate;
var selectCurrentWeek = function() {
window.setTimeout(function() {
$('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active')
}, 1);
}
var $weekPicker = $('.week-picker');
function updateWeekStartEnd() {
var date = $weekPicker.datepicker('getDate') || new Date();
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
}
updateWeekStartEnd();
function updateDateText(inst) {
var dateFormat = inst != 'start' && inst.settings.dateFormat ? inst.settings.dateFormat : $.datepicker._defaults.dateFormat;
console.log(dateFormat)
$('#startDate').text($.datepicker.formatDate(dateFormat, startDate, inst.settings));
$('#endDate').text($.datepicker.formatDate(dateFormat, endDate, inst.settings));
}
updateDateText('start');
$weekPicker.datepicker({
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function (dateText, inst) {
updateWeekStartEnd();
updateDateText(inst);
selectCurrentWeek();
},
beforeShowDay: function (date) {
var cssClass = '';
if (date >= startDate && date <= endDate) cssClass = 'ui-datepicker-current-day';
return [true, cssClass];
},
onChangeMonthYear: function (year, month, inst) {
selectCurrentWeek();
}
});
selectCurrentWeek();
$('.week-picker .ui-datepicker-calendar tr').on('mousemove', function() {
$(this).find('td a').addClass('ui-state-hover');
});
$('.week-picker .ui-datepicker-calendar tr').on('mouseleave', function() {
$(this).find('td a').removeClass('ui-state-hover');
});
});
DEMO:http://jsfiddle.net/eFzG4/
感謝很多:) – ZedBee
這正是我所期待的。但我也需要選擇一些日期。用戶將選擇日期範圍或特定日期。我怎樣才能使它在這個工作? –
@Murali應該相當容易添加一組收音機,或一個複選框,以便用戶可以選擇整個星期或一天。然後,如果它處於白天模式,則不運行上面的任何「星期」代碼 – charlietfl