如何通过传递日期数组来操纵jquery datepicker?

时间:2014-01-26 05:28:41

标签: jquery

我的要求如下;

如果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'];
            }
        }


    });

});

但它不起作用,它在每个日期显示“你好”。如果有人知道解决方案,那么请帮忙,谢谢。

2 个答案:

答案 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)

受到现在删除的答案的启发:

Live Demo

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, '', ''];
    }
});