javascript - 如何在 jquery datepicker 的禁用日期上显示工具提示

标签 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 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>

如上节假日原因添加在标签的标题属性中,但悬停在该日期不显示工具提示

最佳答案

要禁用日期和显示工具提示,请使用 beforeShowDay 选项。

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。 $('#datepicker').click(函数() { changeDisableDateColor(); });

还有 onChangeMonthYear(检测月份或年份的变化)选项

onChangeMonthYear: function(){
  setTimeout(function(){
    changeDisableDateColor();
  });
}

使用 setTimeout 是因为 jquery ui 删除了所有日期项并再次添加,所以我们等到我们有正确的日期。没有 setTimeout var unselectableDate = $('.ui-datepicker-unselectable a'); 将给出之前的日期。

这是 jsfiddle:http://jsfiddle.net/Xx4GS/1225/

希望我回答了这个问题。

编辑:

有一个错误,当日或月是个位数时,即 1-9 那么日期不会匹配并且不会被禁用。

if(month.toString().length == 1){
    month = '0'+month.toString();
}

if(day.toString().length == 1){
    day = '0'+ day.toString();
}

通过在日和月前添加 0 修复了错误。

这是已解决的 js fiddle :http://jsfiddle.net/Lf5p3hct/2/

关于javascript - 如何在 jquery datepicker 的禁用日期上显示工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50839300/

相关文章:

javascript - 将值从 Firebase JS 回调方法 (DAL) 返回到另一个函数( Controller )

javascript - 无法从 $.ajax 迭代 json 数组

javascript - react : How do I use my consumer inside ComponentDidMount to change state?

javascript - 无法将 bootstrap-select 与 Jquery 附加一起用于动态下拉列表

html - 固定在 window 上,但包含在容器内

javascript - <audio> 和 <progress> HTML5 元素的自定义进度条

javascript - jQuery 验证 - 多组

javascript - javascript如何从url解析xml获取属性

html - 使用 th :text in thymeleaf 添加 HTML 标签

javascript - django下拉项超链接在单击时不起作用