0
我有以下問題:我使用jQuery Datepicker,以便用戶可以選擇航班日期。我面臨的唯一問題是,當用戶選擇日期,然後單擊頂部箭頭以轉到上一個月或下一個月,然後用戶選擇的日期(紅色)返回原始。我怎樣才能解決這個問題?請幫忙。我提供一個小提琴:FIDDLE HERE 停止jQuery Datepicker刷新時點擊前一個/下一個月
$(document).ready(function() {
var start_date = "2014-10-04";
var end_date = "2014-10-20";
var date1 = new Date(start_date);
var date2 = new Date(end_date);
var actual_end_date = new Date(end_date);
actual_end_date.setDate(actual_end_date.getDate() + 2);
var pre_nights = 1;
var post_nights = 2;
var msecPerDay = 24 * 60 * 60 * 1000;
date2.setDate(date2.getDate() + 1);
if (post_nights > 0) {
date2 = new Date(date2.getTime() + (msecPerDay * post_nights));
}
var flight_end = new Date(date2.getTime() + msecPerDay);
if (pre_nights > 0) {
date1 = new Date(date1.getTime() - (msecPerDay * pre_nights));
}
var post_night_dates_arr = [];
var pre_night_dates_arr = [];
function date_check() {
$(".ui-state-active").removeClass("ui-state-active");
$('.ui-datepicker-group').each(function() {
$(this).find('.ui-datepicker-calendar tbody td:has(a)').each(function() {
var this_elem = $(this);
var this_date = new Date($(this).data('year') + "-" + ($(this).data('month') + 1) + "-" + $(this).text());
var this_end_date = new Date(this_date);
this_end_date.setDate(this_end_date.getDate() + 1);
var current_this_end_date = this_end_date.getFullYear() + "-" + (this_end_date.getMonth() + 1) + "-" + ("0" + this_end_date.getDate()).slice(-2);
var current_day = this_date.getFullYear() + "-" + (this_date.getMonth() + 1) + "-" + ("0" + this_date.getDate()).slice(-2);
if (pre_night_dates_arr.length > 0 || post_night_dates_arr.length > 0) {
$.each(pre_night_dates_arr, function (key, val) {
if (current_day === val) {
this_elem.addClass("extra_nights_color");
}
});
$.each(post_night_dates_arr, function (key, val) {
if (current_this_end_date === val) {
this_elem.addClass("extra_nights_color");
}
});
}
});
});
}
$("#datepicker").datepicker({
numberOfMonths: 2,
dateFormat: 'yy-mm-dd',
defaultDate: date1,
beforeShowDay: function (date) {
var current_day = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + ("0" + date.getDate()).slice(-2);
var flight_start = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + ("0" + date1.getDate()).slice(-2);
var flight_end_mod = flight_end.getFullYear() + "-" + (flight_end.getMonth() + 1) + "-" + ("0" + flight_end.getDate()).slice(-2);
if (current_day == flight_start) {
var arr = [true, "flight_dept_color"];
} else if (date > date1 && date < date2) {
if (pre_nights > 0) {
pre_night_dates_arr.push(current_day);
pre_nights--;
var arr = [true, "extra_nights_color"];
} else if (post_nights > 0) {
var flight_end_date = new Date(flight_end);
flight_end_date.setDate(flight_end_date.getDate() - post_nights);
var flight_end_date_mod = flight_end_date.getFullYear() + "-" + (flight_end_date.getMonth() + 1) + "-" + ("0" + flight_end_date.getDate()).slice(-2);
if (current_day == flight_end_date_mod) {
post_night_dates_arr.push(current_day);
post_nights--;
var arr = [true, "extra_nights_color"];
} else {
var arr = [true, "tour_dates_color"];
}
} else {
var arr = [true, "tour_dates_color"];
}
} else if (current_day == flight_end_mod) {
var arr = [true, "flight_return_color"];
} else {
var arr = [true, ""];
}
return arr;
},
onSelect: function (dateStr, inst) {
inst.inline = false;
date_check();
console.log("test");
$(".ui-datepicker-calendar tbody td:has(a)").each(function() {
var this_date = $(this).data('year') + "-" + ("0" + ($(this).data('month') + 1)).slice(-2) + "-" + ("0" + $(this).text()).slice(-2);
var date2_mod = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + ("0" + date2.getDate()).slice(-2);
if (pre_night_dates_arr.length > 0) {
if (this_date < pre_night_dates_arr[0]) {
if (this_date == dateStr) {
$(".ui-datepicker-calendar .flight_dept_color").removeClass("flight_dept_color");
$(this).addClass("flight_dept_color");
}
} else if (this_date > date2_mod) {
if (this_date == dateStr) {
$(".ui-datepicker-calendar .flight_return_color").removeClass("flight_return_color");
$(this).addClass("flight_return_color");
}
}
} else {
var flight_start = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + ("0" + date1.getDate()).slice(-2);
if (this_date <= flight_start) {
if (this_date == dateStr) {
$(".ui-datepicker-calendar .flight_dept_color").removeClass("flight_dept_color");
$(this).addClass("flight_dept_color");
}
} else if (this_date > date2_mod) {
if (this_date == dateStr) {
$(".ui-datepicker-calendar .flight_return_color").removeClass("flight_return_color");
$(this).addClass("flight_return_color");
}
}
}
});
}
});
date_check();
$('.ui-corner-all').on('click', function (e) {
date_check();
});
});
您可以縮小問題範圍嗎? – j08691
我要麼不理解,要麼不能重現問題。你能澄清嗎? – Mathletics
基本上,當用戶在「旅遊日期」之前選擇一個日期時,它應該呈現紅色,這是有效的。但是,選擇此日期後會出現問題,然後單擊上一個/下一個月的箭頭。當點擊這些時,選定的紅色日期會回到最初的日期。當單擊prev/next按鈕時,是否有辦法禁用此「刷新」行爲? – CodeGodie