【问题标题】:How to show tooltip on disabled dates of jquery datepicker如何在 jquery datepicker 的禁用日期上显示工具提示
【发布时间】:2018-11-23 03:47:18
【问题描述】:

我正在尝试在某些禁用日期上添加工具提示作为假期原因,但在悬停时无法显示它们。我什至尝试添加自定义悬停功能,但没有成功。

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>

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

【问题讨论】:

    标签: javascript jquery html jquerydatetimepicker


    【解决方案1】:

    要禁用日期和显示工具提示使用选项 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(function() { 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/

    【讨论】:

    • 您将如何实现noWeekends 功能?我希望仅在节假日使用工具提示,因此应该禁用周末。
    • 在 beforeShowDay 中检查周末并将该日期添加到 disabledDates 数组中。这是jsfiddle:jsfiddle.net/49vpmsg6/3
    【解决方案2】:

    我在使用 JQuery 3.3.1 时遇到了同样的问题。我通过添加 CSS 解决了这个问题。
    尝试在您的 CSS 文件中添加以下内容:

    .ui-datepicker-calendar > .ui-state-disabled.Highlighted {
        pointer-events: initial;
    }
    

    这将删除由 CSS 类 ui-state-disabled 设置的属性 pointer-events: none;

    必须将 CSS 中的Hightlighted 部分更改为您添加到beforeShowDay() 中的日期字段的任何 CSS 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 2016-12-07
      • 2014-11-11
      • 2016-06-15
      相关资源
      最近更新 更多