【问题标题】:jQuery UI Datepicker - allow only certain weekdaysjQuery UI Datepicker - 只允许某些工作日
【发布时间】:2011-05-27 22:27:31
【问题描述】:

我在一个大型项目中使用 jquery UI datepicker,现在我意识到我只需要在某些区域允许某些工作日。我阅读了他们的文档,但没有找到任何关于它的内容。我知道有一些用于 jq 的 datepickers 脚本可以做到这一点,但如果可能的话,我不想为此使用任何额外的脚本。 任何人都知道任何解决方法吗?也许我在他们的文档中被误解了?

注意:期望行为示例:http://codeasp.net/blogs/raghav_khunger/microsoft-net/1088/jquery-datepicker-disable-specific-weekdays

提前感谢您的帮助, 干杯 佩德罗

【问题讨论】:

  • 您发布的链接包含您问题的完整答案..
  • @Andrew,是的……非常直截了当。
  • 不幸的是,链接是死胡同。

标签: jquery jquery-ui datepicker uidatepicker jquery-ui-datepicker


【解决方案1】:
$( ".datepicker.future" ).datepicker('option','beforeShowDay',function(date){
    var td = date.getDay();
    var ret = [(date.getDay() != 0 && date.getDay() != 6),'',(td != 'Sat' && td != 'Sun')?'':'only on workday'];
    return ret;
});

【讨论】:

  • td 变量被分配了一个整数值,但它正在与字符串进行比较。我想你想要这个:var td = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'][date.getDay()];
【解决方案2】:

@Jan Thomas 忘记添加变量 td。正确的代码是:

$( ".datepicker.future" ).datepicker('option','beforeShowDay',function(date){
    var td = date.getDay();
    var ret = [(date.getDay() != 0 && date.getDay() != 6),'',(td != 'Sat' && td != 'Sun')?'':'only on workday'];
    return ret;
});

【讨论】:

  • 如果它是给定日期的数字表示,td 可以是“sat”吗?一个错误,或者我不明白。
  • td 变量被分配了一个整数值,但它正在与字符串进行比较。我想你想要这个:var td = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'][date.getDay()];
【解决方案3】:
$('selector').datepicker({
beforeShowDay: $.datepicker.noWeekends // disable weekends
});

【讨论】:

    【解决方案4】:

    来自文档:

    演出日前: 该函数将日期作为参数,并且必须返回一个数组,其中 [0] 等于 true/false 指示此日期是否可选,[1] 等于 CSS 类名称或默认表示为 '' , 和 [2] 此日期的可选弹出工具提示。日期选择器中的每一天都会在显示之前调用它。

    例如,请看这里:

    http://codeasp.net/blogs/raghav_khunger/microsoft-net/1088/jquery-datepicker-disable-specific-weekdays

    【讨论】:

    【解决方案5】:

    Jean Mallet 的回答 (beforeShowDay: $.datepicker.noWeekends) 简单而优雅。

    但是,如果您已经使用 beforeShowDay 调用函数(就像我的情况一样),那么您可以通过以下方法禁用一周中的某些天(在本例中为星期日和星期六),并与你的其他功能:

    beforeShowDay: function (date) {
    
        /* your other function code here */
    
        if (date.getDay() > 0 && date.getDay() < 6) {
            return [true, ''];
        } else {
            return [false, ''];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多