cle*_*tus 192 javascript jquery jquery-ui jquery-ui-datepicker
我使用日期选择器来选择预约日.我已经将日期范围设置为仅适用于下个月.这很好.我想从可用的选择中排除星期六和星期日.可以这样做吗?如果是这样,怎么样?
Ada*_*ire 242
有一个beforeShowDay选项,它为每个日期调用一个函数,如果允许日期则返回true,否则返回false.来自文档:
beforeShowDay
该函数将日期作为参数,并且必须返回一个数组,其中[0]等于true/false,指示此日期是否可选,1等于CSS类名称或''作为默认表示.在显示之前,会在datepicker中调用它.
在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, ''];
}
Run Code Online (Sandbox Code Playgroud)
存在一个内置函数,称为noWeekends,它阻止选择周末日.
$(".selector").datepicker({ beforeShowDay: $.datepicker.noWeekends })
Run Code Online (Sandbox Code Playgroud)
要结合这两者,你可以做一些事情(假设nationalDays上面的函数):
$(".selector").datepicker({ beforeShowDay: noWeekendsOrHolidays})
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
Run Code Online (Sandbox Code Playgroud)
更新:请注意,从jQuery UI 1.8.19开始,beforeShowDay选项也接受可选的第三个参数,弹出工具提示
小智 41
如果您不希望周末出现,只需:
CSS
th.ui-datepicker-week-end,
td.ui-datepicker-week-end {
display: none;
}
Run Code Online (Sandbox Code Playgroud)
oro*_*olo 26
这些答案非常有用.谢谢.
我在下面的贡献添加了一个数组,其中多天可以返回false(我们每周二,周三和周四关闭).我将特定日期加上年份和无周末功能捆绑在一起.
如果您想要周末休息,请将[Saturday],[Sunday]添加到closedDays数组.
$(document).ready(function(){
$("#datepicker").datepicker({
beforeShowDay: nonWorkingDates,
numberOfMonths: 1,
minDate: '05/01/09',
maxDate: '+2M',
firstDay: 1
});
function nonWorkingDates(date){
var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
var closedDates = [[7, 29, 2009], [8, 25, 2010]];
var closedDays = [[Monday], [Tuesday]];
for (var i = 0; i < closedDays.length; i++) {
if (day == closedDays[i][0]) {
return [false];
}
}
for (i = 0; i < closedDates.length; i++) {
if (date.getMonth() == closedDates[i][0] - 1 &&
date.getDate() == closedDates[i][1] &&
date.getFullYear() == closedDates[i][2]) {
return [false];
}
}
return [true];
}
});
Run Code Online (Sandbox Code Playgroud)
Mik*_*ace 21
datepicker内置了此功能!
$( "#datepicker" ).datepicker({
beforeShowDay: $.datepicker.noWeekends
});
Run Code Online (Sandbox Code Playgroud)
http://api.jqueryui.com/datepicker/#utility-noWeekends
Cle*_*ter 13
每个人都喜欢的解决方案看起来非常激烈......我个人认为这样做更容易:
var holidays = ["12/24/2012", "12/25/2012", "1/1/2013",
"5/27/2013", "7/4/2013", "9/2/2013", "11/28/2013",
"11/29/2013", "12/24/2013", "12/25/2013"];
$( "#requestShipDate" ).datepicker({
beforeShowDay: function(date){
show = true;
if(date.getDay() == 0 || date.getDay() == 6){show = false;}//No Weekends
for (var i = 0; i < holidays.length; i++) {
if (new Date(holidays[i]).toString() == date.toString()) {show = false;}//No Holidays
}
var display = [show,'',(show)?'':'No Weekends or Holidays'];//With Fancy hover tooltip!
return display;
}
});
Run Code Online (Sandbox Code Playgroud)
这样你的日期就是人类可读的.它并没有那么不同,这对我来说更有意义.
此版本的代码将使您从sql数据库获取假日日期并禁用UI Datepicker中的指定日期
$(document).ready(function (){
var holiDays = (function () {
var val = null;
$.ajax({
'async': false,
'global': false,
'url': 'getdate.php',
'success': function (data) {
val = data;
}
});
return val;
})();
var natDays = holiDays.split('');
function nationalDays(date) {
var m = date.getMonth();
var d = date.getDate();
var y = date.getFullYear();
for (var i = 0; i ‘ natDays.length-1; i++) {
var myDate = new Date(natDays[i]);
if ((m == (myDate.getMonth())) && (d == (myDate.getDate())) && (y == (myDate.getFullYear())))
{
return [false];
}
}
return [true];
}
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
$(function() {
$("#shipdate").datepicker({
minDate: 0,
dateFormat: 'DD, d MM, yy',
beforeShowDay: noWeekendsOrHolidays,
showOn: 'button',
buttonImage: 'images/calendar.gif',
buttonImageOnly: true
});
});
});
Run Code Online (Sandbox Code Playgroud)
在sql中创建一个数据库,并以MM/DD/YYYY格式将假日日期作为Varchar放入文件中将以下内容放入getdate.php文件中
[php]
$sql="SELECT dates FROM holidaydates";
$result = mysql_query($sql);
$chkdate = $_POST['chkdate'];
$str='';
while($row = mysql_fetch_array($result))
{
$str .=$row[0].'';
}
echo $str;
[/php]
Run Code Online (Sandbox Code Playgroud)
快乐的编码!!!! :-)
您可以使用noWeekends功能禁用周末选择
$(function() {
$( "#datepicker" ).datepicker({
beforeShowDay: $.datepicker.noWeekends
});
});
Run Code Online (Sandbox Code Playgroud)