我的要求如下;
如果datepicker的日期与使用数组的传递日期匹配,那么它应该在悬停时显示hello,否则显示已售出。 我尝试了以下代码
var rateArray = [new Date(2014, 1, 1), new Date(2014, 1, 2)]; // just some dates.
var date = new Date();
$(document).ready(function () {
$("#test").datepicker({
beforeShowDay: function (date) {
if ($.inArray(date, rateArray)) {
return [true, 'tiptip', 'hello'];
}
else {
return [false, 'tiptip', 'Sold'];
}
}
});
});
但它不起作用,它在每个日期显示“你好”。如果有人知道解决方案,那么请帮忙,谢谢。
答案 0 :(得分:1)
尝试
//use a string representation for the date
var rateArray = ['01 Jan 2014', '02 Jan 2014']; // just some dates.
$(document).ready(function () {
$("#test").datepicker({
beforeShowDay: function (date) {
//convert the date to a string format same as the one used in the array
var string = $.datepicker.formatDate('dd M yy', date)
// $.inArray() returns -1 if the item is not found so change the condition accordingly
if ($.inArray(string, rateArray) > -1) {
return [true, 'tiptip', 'hello'];
} else {
return [false, 'tiptip', 'Sold'];
}
}
});
});
演示:Fiddle
答案 1 :(得分:0)
受到现在删除的答案的启发:
var dates = {};
dates[new Date('02/13/2014').getTime()]='Holiday1';
dates[new Date('02/14/2014').getTime()]='Holiday2';
dates[new Date('02/15/2014').getTime()]='Holiday3';
$('#txtDate').datepicker({
beforeShowDay: function(date) {
var hlText = dates[date.getTime()];
return (hlText)?[true, "tiptip", hlText]: [true, '', ''];
}
});