在日期选择器中禁用星期五和星期六

Disable Friday and Saturday In Datepicker

我正在尝试编辑 jquery.ui.datepicker 中的 beforeShowDay 函数。 这是我要替换的日期选择器中的原始 beforeShowDay 行:

beforeShowDay: null, // Function that takes a date and returns an array with
        // [0] = true if selectable, false if not, [1] = custom CSS class 
        name(s) or '',
        // [2] = cell title (optional), e.g. $.datepicker.noWeekends

我四处搜索,试图找到正确的代码来替换它,但没有成功。 我找到了这个 fiddle; disable 1 day in datepicker

我编辑了这个 fiddle 并使用以下代码成功禁用了周五和周六:

    $("#datepicker").datepicker({
    beforeShowDay: function(date) {
        return [date.getDay() == 0 || date.getDay() == 1 || date.getDay() == 2 || date.getDay() == 3 || date.getDay() == 4 ] ;
    }
});

然而,当我将其复制并粘贴到 jquery.ui.datepicker 时,日历不起作用并且我收到控制台错误(未捕获的语法错误:意外标记)。

我正在做的是将原始 beforeShowDate 替换为以下内容:

beforeShowDay: function(date) { return [date.getDay() == 0 || date.getDay() == 1 || date.getDay() == 2 || date.getDay() == 3 || date.getDay() == 4 ] ; }

任何人都可以告诉我做错了什么以及如何让它正常运行吗?

daysOfWeekDisabled

Days of the week that should be disabled. Values are 0 (Sunday) to 6 (Saturday). Multiple values should be comma-separated. Example: disable weekends: '06' or '0,6' or [0,6].

为此使用daysOfWeekDisabled

$('#datepicker').datepicker({
    daysOfWeekDisabled: [5,6]
});

Working fiddle

编辑

我在阅读和 post 上面关于 bootstarp datepicker 的回答时犯了错误。

对于 Jquery UI 日期选择器试试这个

beforeShowDay: function(d) {
   return [!(d.getDay()==0||d.getDay()==6)]
}

Working fiddle

你不应该直接编辑 jQuery UI 插件

如果您确实需要,则必须粘贴此代码以替换 null。但不推荐

function(date) {
    var show = true;
    if(date.getDay()==6||date.getDay()==0) show=false;
    return [show];
},//don't forget comma after the function

正确的做法是在您自己的 js 文件中配置 jquery ui 日期选择器时传递函数。

$("#datepicker").datepicker({
    beforeShowDay: function(date) {
       var show = true;
       if(date.getDay()==6||date.getDay()==0) show=false
       return [show];
    }
});

你可以试试这个,

$("#datepicker").datepicker({
   beforeShowDay: function(date) {
    var day = date.getDay();
    return [(day != 5 && day != 6), ''];
  }
});

http://jsfiddle.net/nWAnz/238/