通过 JQuery datePicker 中的 beforeShowDay 返回多个值
Returning multiple value through beforeShowDay in JQuery datePicker
我一直在尝试通过 beforeShowDay
禁用多个日期,其中一些是日期,一些是星期几。
不幸的是,我只能禁用其中一个。
var unavailableDates = ["2/6/2020", "4/6/2020"];
beforeShowDay: function(date) {
// Disabling Sundays
var day = date.getDay();
return [day != 0,''];
// Disabling Dates Array
findDate = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
if ($.inArray(findDate, unavailableDates) == -1) {
return [true, ""];
} else {
return [false, "", "Unavailable"];
}
}
PS,我需要在默认情况下禁用所有星期日,但只有在数组中找到日期。
请尝试下面的代码,其中我禁用了数组中的日期并向日期元素添加了突出显示 class。
<html>
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script>
$(function () {
var dateArray = [ "2020-03-17", "2020-03-18","2020-03-20"];
$("#datepicker").datepicker({
dateFormat: 'yy-mm-dd',
beforeShowDay: function (date) {
var day = date.getDay();
// First convert all values in dateArray to date Object and compare with current date
var dateFound = dateArray.find(function(item) {
var formattedDate = new Date(item);
return date.toLocaleDateString() === formattedDate.toLocaleDateString();
})
// check if date is in your array of dates
if(dateFound) {
// if it is return the following.
return [false, 'css-class-to-highlight', 'tooltip text'];
} else {
// default
// Disable all sundays
return [(day != 0), '', ''];
}
}
});
});
</script>
<style type="text/css">
.css-class-to-highlight a{
background-color: blue !important;
color: #fff !important;
}
</style>
</head>
<body>
<form>
<div>
<div id="datepicker"></div>
</div>
</form>
</body>
</html>
我一直在尝试通过 beforeShowDay
禁用多个日期,其中一些是日期,一些是星期几。
不幸的是,我只能禁用其中一个。
var unavailableDates = ["2/6/2020", "4/6/2020"];
beforeShowDay: function(date) {
// Disabling Sundays
var day = date.getDay();
return [day != 0,''];
// Disabling Dates Array
findDate = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
if ($.inArray(findDate, unavailableDates) == -1) {
return [true, ""];
} else {
return [false, "", "Unavailable"];
}
}
PS,我需要在默认情况下禁用所有星期日,但只有在数组中找到日期。
请尝试下面的代码,其中我禁用了数组中的日期并向日期元素添加了突出显示 class。
<html>
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script>
$(function () {
var dateArray = [ "2020-03-17", "2020-03-18","2020-03-20"];
$("#datepicker").datepicker({
dateFormat: 'yy-mm-dd',
beforeShowDay: function (date) {
var day = date.getDay();
// First convert all values in dateArray to date Object and compare with current date
var dateFound = dateArray.find(function(item) {
var formattedDate = new Date(item);
return date.toLocaleDateString() === formattedDate.toLocaleDateString();
})
// check if date is in your array of dates
if(dateFound) {
// if it is return the following.
return [false, 'css-class-to-highlight', 'tooltip text'];
} else {
// default
// Disable all sundays
return [(day != 0), '', ''];
}
}
});
});
</script>
<style type="text/css">
.css-class-to-highlight a{
background-color: blue !important;
color: #fff !important;
}
</style>
</head>
<body>
<form>
<div>
<div id="datepicker"></div>
</div>
</form>
</body>
</html>