更新我的代碼以反映我將兩個語句合併的嘗試。爲語句添加第二個函數
function unavailableDays(date) {
//date array to be disabled
var disabledDays = ["1963-3-31", "1965-9-18", "1965-9-19",
"1965-10-2", "1965-10-3", "1965-10-9", "1965-10-10"];
var yy = date.getFullYear(), mm = date.getMonth(), dd = date.getDate();
for (i = 0; i < disabledDays.length; i++) {
if($.inArray(yy + '-' + (mm+1) + '-' + dd,disabledDays) != -1 || new Date() < date) {
return [false];
}
}
return [true];
//date range to be disabled
var first = new Date("1978-8-10");
var last = new Date("1978-11-05");
var unavailableRange = [];
var yy = date.getFullYear(), mm = date.getMonth(), dd = date.getDate();
for(j = first; j < last; j.setDate(j.getDate() + 7)){
if($.inArray(yy + '-' + (mm+1) + '-' + dd,unavailableRange) != -1 || new Date() < date) {
return [false];
}
}
return [true];
}
我試圖用日期範圍初始變量納入unavailableDays功能下面,這樣我可以在日期選擇器中beforeShowDay用它來禁用日期。我試過創建一個單獨的函數,但這對beforeShowDate不起作用,所以我認爲它需要在裏面。
var first = new Date("1978-08-10");
var last = new Date("1978-11-05");
var dates = [];
for (var i = first; i < last; i.setDate(i.getDate() + 7))
dates.push(new Date);
var disabledDays = ["1963-2-17", "1963-2-24", "1963-3-3", "1963-3-10", "1963-3-17", "1963-3-24", "1963-3-31", "1965-9-18", "1965-9-19", "1965-10-2", "1965-10-3", "1965-10-9", "1965-10-10"];
function unavailableDays(date) {
var yy = date.getFullYear(),
mm = date.getMonth(),
dd = date.getDate();
for (i = 0; i < disabledDays.length; i++) {
if ($.inArray(yy + '-' + (mm + 1) + '-' + dd, disabledDays) != -1 || new Date() < date) {
return [false];
}
}
return [true];
}
$(document).ready(function() {
$('.selector').datepicker({
inline: true,
dateFormat: 'yy-mm-dd',
constrainInput: true,
changeYear: true,
changeMonth: true,
gotoCurrent: true,
minDate: new Date(1962, 1 - 1, 1),
//months are index-based(1-1)
maxDate: new Date(2011, 10 - 1, 24),
yearRange: '-60y',
beforeShowDay: unavailableDays,
onSelect: function (dateText, inst) {
$("#img").attr("src", "http://www.example.com" + dateText + ".jpg");
var chosenDates = $.datepicker.parseDate('yy-mm-dd', dateText);
var backToString = $.datepicker.formatDate('MM dd' + ',' + ' yy', chosenDates);
$('.info').html('You are viewing:' + '<br />' + backToString);
}
});
當評估爲布爾值時,'[false]'和'[true]'總是'true'。你在那裏返回數組。 – Pointy 2012-04-05 21:05:05