我有一个jQuery UI Datepicker的问题,我试图通过ajax调用来确定给定月份中的空闲日期。Jquery datepicker beforeShowDay浏览器不一致和更新问题
问题是双重的 -
的beforeShowDay只似乎在Chrome,不FF或IE正确执行时,自由天类根本就没有被在这些浏览器中加入。
即使在Chrome中,当滚动到前一个月时,直到返回到该月份才会添加免费日班,换句话说,免费日在该月的第一个视图上不会突出显示。这似乎不是一个月前进的问题。
的JavaScript
// declare freeDays global
var freeDays = [];
// perform initial json request for free days
fetchFreeDays();
$(document).ready(function()
{
// fairly standard configuration, importantly containing beforeShowDay and onChangeMonthYear custom methods
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true,
dateFormat: 'DD, d MM, yy',
altField: '#date_due',
altFormat: 'yy-mm-dd',
beforeShowDay: highlightDays,
onChangeMonthYear: fetchFreeDays,
firstDay: 1 // rows starts on Monday
});
});
// query for free days in datepicker
function fetchFreeDays(year, month)
{
var start_date = '';
// if a month and year were supplied, build a start_date in yyyy-mm-dd format
if (year != undefined && month != undefined) {
start_date = year +'-';
start_date += month +'-';
start_date += '01';
}
$.getJSON("ajax.todos.php?start_date="+ start_date, function(data){
$.each(data, function(index, value) {
freeDays.push(value.freeDate); // add this date to the freeDays array
});
});
}
// runs for every day displayed in datepicker, adds class and tooltip if matched to days in freeDays array
function highlightDays(date)
{
for (var i = 0; i < freeDays.length; i++) {
if (new Date(freeDays[i]).toString() == date.toString()) {
return [true, 'free-day', 'no to-do items due']; // [0] = true | false if this day is selectable, [1] = class to add, [2] = tooltip to display
}
}
return [true, ''];
}
PHP
// ajax.todos.php
$i = 0; // counter prevents infinite loop
$cutoff = '61'; // limit on timespan (in days)
$result = array();
// if date is provided, use it, otherwise default to today
$start_date = (!empty($start_date)) ? mysql_real_escape_string($start_date) : date('Y-m-d');
$check_date = $start_date;
$end_date = date('Y-m-d', strtotime("$start_date +$cutoff days")); // never retrieve more than 2 months
while ($check_date != $end_date)
{
// check if any incomplete todos exist on this date
if (mysql_result(mysql_query("SELECT COUNT(id) FROM " . DB_TODOS . " WHERE date_due = '$check_date'"), 0) == 0)
{
$result[] = array('freeDate' => $check_date);
}
// +1 day to the check date
$check_date = date('Y-m-d', strtotime("$check_date +1 day"));
// break from loop if its looking like an infinite loop
$i++;
if ($i > $cutoff) break;
}
header('Content-type: application/json');
echo json_encode($result);
CSS
/* override free days background in jquery ui datepicker */
.free-day {
background: #2e9500;
}
.free-day a {
opacity: 0.7;
}
我几个月前写的教程这个是可能有一些额外的信息,这是here 。
还是不太有... 我一直有实施上的getJSON回调麻烦。 现在我试着切换到$ .ajax与async:false选项,这似乎解决了在免费天数组填充前切换几个月的问题,但我仍然无法在FF或IE中工作之前的ShowDays。 我已经把$('#datepicker')。datepicker()放在ajax成功:回调中。 – 2011-03-28 07:11:54
在firebug中,我可以看到两者都很好的请求和响应,因此问题出现在highlightDays中。是否有任何理由为什么toString行会在不同的浏览器中被区别对待? – 2011-03-28 07:27:44