【问题标题】:Is there a way to make jQuery datepicker work without years?有没有办法让 jQuery datepicker 不用几年就能工作?
【发布时间】:2013-11-21 18:33:12
【问题描述】:

我想在“历史上的这一天”项目中使用日期选择器。

这个想法是用户选择一天,比如“11 月 20 日”,该项目会向他展示不同年份的那一天发生的历史事件。

鉴于他们选择的是通用日期,而不是特定日期(例如“2013 年 11 月 20 日”),我希望日期选择器与年份无关。

Several people have asked 类似的东西,但他们正在寻找隐藏年份的方法。

我不想隐藏年份。我希望选择器忽略年份。

不应有日期标签(“星期一”、“星期二”等),并且第一周开始时不应有空白日。如果是 2 月,它将列出 29 天。

所以,不要像这样......

NOVEMBER
Mo  Tu  We  Th  Fr  Sa  Su
--  --  --  1   2   3   4
5   6   7   8   9   10  11
12  13  14  15  16  17  18

...应该是这样的

NOVEMBER
1   2   3   4   5   6   7
8   9   10  11  12  13  14
15  16  17  18  19  20  21

有没有办法修改 jQuery UI 的 datepicker 使其像这样工作?或者另一个为其日期选择器提供无年份选项的库?

【问题讨论】:

  • “有没有办法修改 jQuery UI 的 datepicker 使其像这样工作”,但这需要修改/扩展小部件的私有方法。这种功能不是内置的。
  • 这听起来像构建自己的比扩展 jQuery UI 更快,因为您的要求非常简单。
  • 需要年份来确定特定日期在一周中的哪一天。您可能只想构建一个静态日历并更改月份名称。 Datepicker 对于您的方案可能有点过分。

标签: javascript jquery jquery-ui date datepicker


【解决方案1】:

您可以通过重写 Date.prototype.getDay() 方法来做到这一点:

JS:

$(function() {
    //override getDay()
    Date.prototype.getDay = function() {
        //change getDay() so that the first day of the month is always at 
        // the beginning of the week
        return ((this.getDate()-1) % 7);
    }

    //create the datepicker that does not display year
    $( "#datepicker" ).datepicker({dateFormat: "d MM"});
});

CSS(在日期选择器中隐藏年份和日期):

.ui-datepicker-year, .ui-datepicker-calendar thead {
    display: none;
}

Fiddle

【讨论】:

  • 这绝对有效,但可能会导致其他脚本出现严重问题。最好将此更改本地化到 jQuery UI 的输出。
  • @Mathletics 我同意,这可能会导致一些问题。如果您不知道有人已经将其卡在代码中的某个地方,它也可能会导致调试起来很糟糕。
猜你喜欢
  • 1970-01-01
  • 2011-04-05
  • 2015-09-20
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多