填写日期的文本框,但不要在JQuery Date Picker中显示周末 [英] Fill text box with date but don't show weekends in JQuery Date Picker

查看:156
本文介绍了填写日期的文本框,但不要在JQuery Date Picker中显示周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在页面加载时在日期文本字段中显示日期或预加载日期。

I am using the below code to display date or preload date in the date text field when the page is loaded.

$(function() {
    $("#datepicker").datepicker({
        dateFormat: "yy-mm-dd"
    }).datepicker("setDate", "0");
});

但是,我想跳过周末和周日显示/提前日期字段。

But I want to skip Weekend days Saturday and Sunday displaying/prelading the date field.

请帮助!

推荐答案

有beforeShowDay选项,它具有一个功能被称为每个日期,如果允许日期返回true,否则返回false。从文档中:

There is the beforeShowDay option, which takes a function to be called for each date, returning true if the date is allowed or false if it is not. From the docs:

beforeShowDay

beforeShowDay

该函数将日期作为参数,必须返回一个[0]等于true / false的数组,表示是否此日期是可选择的,1等于CSS类名称或默认演示文稿的。

The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable and 1 equal to a CSS class name(s) or '' for the default presentation. It is called for each day in the datepicker before is it displayed.

在datepicker中显示一些国定假日。

Display some national holidays in the datepicker.

$(".selector").datepicker({ beforeShowDay: nationalDays})   

natDays = [
  [1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'],
  [4, 27, 'za'], [5, 25, 'ar'], [6, 6, 'se'],
  [7, 4, 'us'], [8, 17, 'id'], [9, 7, 'br'],
  [10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke']
];

function nationalDays(date) {
    for (i = 0; i < natDays.length; i++) {
      if (date.getMonth() == natDays[i][0] - 1
          && date.getDate() == natDays[i][1]) {
        return [false, natDays[i][2] + '_day'];
      }
    }
  return [true, ''];
}

一个内置函数存在,称为noWeekend,阻止选择周末日。

One built in function exists, called noWeekends, that prevents the selection of weekend days.

$(".selector").datepicker({ beforeShowDay: $.datepicker.noWeekends })
To combine the two, you could do something like (assuming the nationalDays function from above):

$(".selector").datepicker({ beforeShowDay: noWeekendsOrHolidays})   

function noWeekendsOrHolidays(date) {
    var noWeekend = $.datepicker.noWeekends(date);
    if (noWeekend[0]) {
        return nationalDays(date);
    } else {
        return noWeekend;
    }
}

更新:请注意从jQuery UI 1.8.19开始,beforeShowDay选项也接受可选的第三个Paremeter,一个弹出工具提示

Update: Note that as of jQuery UI 1.8.19, the beforeShowDay option also accepts an optional third paremeter, a popup tooltip

您也可以使用它假期

这篇关于填写日期的文本框,但不要在JQuery Date Picker中显示周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆