Javascript 如何显示jquery datepicker禁用日期的工具提示
我试图添加一些禁用日期的工具提示作为假日原因,但悬停时无法显示它们。我甚至尝试添加自定义悬停功能,但没有成功Javascript 如何显示jquery datepicker禁用日期的工具提示,javascript,jquery,html,jquerydatetimepicker,Javascript,Jquery,Html,Jquerydatetimepicker,我试图添加一些禁用日期的工具提示作为假日原因,但悬停时无法显示它们。我甚至尝试添加自定义悬停功能,但没有成功 availableDates = ["09/04/2018", "09/05/2018", "09/06/2018", "08/31/2018", "09/01/2018"]; holidays_dates = { "09/02/2018": "sgsdf", "05/27/2018": "home sick", "08/20/2018": "fcg", "05/26/2018": "i
availableDates = ["09/04/2018", "09/05/2018", "09/06/2018", "08/31/2018", "09/01/2018"];
holidays_dates = { "09/02/2018": "sgsdf", "05/27/2018": "home sick", "08/20/2018": "fcg", "05/26/2018": "i dont know", "05/25/2018": "home sick" }
function available(date) {
var moth = String(date.getMonth() + 1);
if (moth.length == 1) {
moth = "0" + moth;
}
var dat = String(date.getDate());
if (dat.length == 1) {
dat = "0" + dat;
}
dmy = moth + "/" + dat + "/" + date.getFullYear();
if ($.inArray(dmy, availableDates) != -1) {
return [true, ""];
} else if (dmy in holidays_dates) {
console.log(dmy)
return [false, "Highlighted", holidays_dates[dmy]];
} else {
return [false, ""];
}
}
$("#datepicker").datepicker({
beforeShowDay: function (dt) {
return available(dt);
},
changeMonth: true,
changeYear: true,
onSelect: function (dateText) {
getslots(dateText, selected_consultant);
}
});
holidays_dates变量由禁用的日期和原因组成,它完美地将原因添加到元素的标题中
<td class=" ui-datepicker-unselectable ui-state-disabled Highlighted" title="home sick">
<span class="ui-state-default">25</span>
</td>
25
如上所述,假日原因添加在标记的标题属性中,但在该日期悬停不会显示工具提示。要禁用日期和显示工具提示,请在ShowDay之前使用选项
beforeShowDay: function (date) {
var formattedDate = $.datepicker.formatDate('mm/dd/yy', date),
checkDisable = disabledDates.indexOf(formattedDate) == -1;
if(checkDisable == true){
return [checkDisable];
}else{
//add ui-datepicker-unselectable class to the disabled date.
return [!checkDisable,'ui-datepicker-unselectable',holidays_dates[formattedDate]];
}
}
并自定义禁用日期的css
function changeDisableDateColor(){
var unselectableDate = $('.ui-datepicker-unselectable a');
unselectableDate.css({
background: 'red',
cursor: 'context-menu',
border: 0
});
}
单击日期字段时调用上述函数,即changeDisableDateColor。
$(“#日期选择器”)。单击(函数(){
changeDisableDateColor();
});代码>
以及onChangeMonthYear(检测月份或年份的变化)选项
之所以使用setTimeout,是因为jQueryUI会删除所有日期项并再次添加,所以我们会等到找到正确的日期。没有setTimeoutvar unselectableDate=$('.ui datepicker unselectable a')代码>将给出以前的日期
以下是JSFIDLE:
希望我回答了这个问题
编辑:
有一个错误,当日或月为一位数ie 1-9时,则日期不匹配,不会被禁用
if(month.toString().length == 1){
month = '0'+month.toString();
}
if(day.toString().length == 1){
day = '0'+ day.toString();
}
通过在日和月的前面添加0修复了此错误
以下是解决了的js问题:我在JQuery 3.3.1中遇到了同样的问题。我通过添加CSS解决了这个问题
尝试在CSS文件中添加以下内容:
.ui日期选择器日历>.ui-state-disabled.Highlighted{
指针事件:初始;
}
这将删除属性指针事件:无由CSS类设置的代码>禁用ui状态
CSS中的Hightlighted
部分必须更改为您在beforeShowDay()中添加到日期字段的任何CSS类
您将如何实现noWeekends
功能?我希望工具提示仅适用于假日,因此应禁用周末。请在beforeShowDay中检查周末,并将该日期添加到disabledDates数组中。以下是JSFIDLE:
if(month.toString().length == 1){
month = '0'+month.toString();
}
if(day.toString().length == 1){
day = '0'+ day.toString();
}