我有一个周选择器[FIDDLE],我无法让它在加载时选择当前周。到目前为止,我只选择当天。
另外,有没有比这更简单的方法来做一周选择?
以下是代码:
var startDate;
var endDate;
var selectCurrentWeek = function (where) {
console.log(where);
window.setTimeout(function () {
$('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active');
}, 1);
}
$('.week-picker').datepicker({
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function (dateText, inst) {
var date = $(this).datepicker('getDate');
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;
$('#startDate').text($.datepicker.formatDate(dateFormat, startDate, inst.settings));
$('#endDate').text($.datepicker.formatDate(dateFormat, endDate, inst.settings));
selectCurrentWeek("week-picker");
},
beforeShowDay: function (date) {
var cssClass = '';
if (date >= startDate && date <= endDate)
cssClass = 'ui-datepicker-current-day';
return [true, cssClass];
},
onChangeMonthYear: function (year, month, inst) {
selectCurrentWeek("change");
}
});
$(document).on('mousemove', '.week-picker .ui-datepicker-calendar tr', function () { $(this).find('td a').addClass('ui-state-hover'); });
$(document).on('mouseleave', '.week-picker .ui-datepicker-calendar tr', function () { $(this).find('td a').removeClass('ui-state-hover'); });
答案 0 :(得分:-1)
这是一种贫民窟的方式,但它有效。基本上,您希望在当天触发点击事件。所以我添加到我的$(document).ready函数:
$(document).ready(function () {
$(".ui-datepicker-today").trigger("click");
});
如果有人有更好的解决方案,那就太棒了。