jQuery UI日期选择器-日期范围-突出显示间隔天数

jQuery UI日期选择器-日期范围-突出显示间隔天数,jquery,jquery-ui,datepicker,highlighting,date-range,Jquery,Jquery Ui,Datepicker,Highlighting,Date Range,我正在寻找一种方法,在鼠标悬停时突出显示两次输入之间的日期范围 这个例子几乎实现了我想要实现的目标: 唯一的区别是,所选范围的高亮显示应该在两个单独的日期选择器和鼠标悬停时进行 有什么建议吗 更新: 好的,再详细一点: 从第一个日期选择器中选择日期后,第二个日期选择器应突出显示先前选择的日期。如果将鼠标悬停在上一个选定日期之后的某一天,则应通过添加类突出显示这两天之间的所有日期 更新: 这就是我取得的成绩: $("#input-service_date_leave, #input-

我正在寻找一种方法,在鼠标悬停时突出显示两次输入之间的日期范围

这个例子几乎实现了我想要实现的目标:

唯一的区别是,所选范围的高亮显示应该在两个单独的日期选择器和鼠标悬停时进行

有什么建议吗


更新:

好的,再详细一点:

从第一个日期选择器中选择日期后,第二个日期选择器应突出显示先前选择的日期。如果将鼠标悬停在上一个选定日期之后的某一天,则应通过添加类突出显示这两天之间的所有日期


更新: 这就是我取得的成绩:

    $("#input-service_date_leave, #input-service_date_return").datepicker({
        rangeSelect: true,
        beforeShow: customRange,
        onSelect: customRange,
    });

    function customRange(input) {
        if (input.id == "input-service_date_leave") {

            $("#ui-datepicker-div td").die();

            if (selectedDate != null) { 
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
        if (input.id == "input-service_date_return") {

            $("#ui-datepicker-div td").live({
                mouseenter: function() {
                    $(this).prevAll("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                },
                mouseleave: function() {
                    $("#ui-datepicker-div td").removeClass("highlight");
                }
            });

            var selectedDate = $("#input-service_date_leave").datepicker("getDate");                
            if (selectedDate != null) { 
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
    }

唯一的问题是,实时事件只突出显示当前悬停行的td,而不是之前行的td


有什么想法吗?

我给你的剧本加了一点。我的工作很有魅力。看一看,让我知道

    $("#input-service_date_leave, #input-service_date_return").datepicker({
        rangeSelect: true,
        beforeShow: customRange,
        onSelect: customRange,
    });

    function customRange(input) {
        if (input.id == "input-service_date_leave") {

            $("#ui-datepicker-div td").die();

            if (selectedDate != null) {
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
        if (input.id == "input-service_date_return") {

            $("#ui-datepicker-div td").live({
                mouseenter: function() {
                    $(this).parent().addClass("finalRow");
                    $(".finalRow").prevAll().find("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                    $(this).prevAll("td:not(.ui-datepicker-unselectable)").addClass("highlight");
               },
                mouseleave: function() {
                    $(this).parent().removeClass("finalRow");
                    $("#ui-datepicker-div td").removeClass("highlight");
                }
            });

            var selectedDate = $("#input-service_date_leave").datepicker("getDate");                
            if (selectedDate != null) {
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
    }

编辑:此脚本在jquery 3上不起作用。但是,它在版本1和版本2上确实有效

是使用两个日期表(多个月)执行此操作的示例


此处为内联日期选择器创建了日期范围悬停示例:

$(函数(){
变量日期选择器={
容器:$(“#日期选择器”),
日期格式:“mm/dd/yy”,
日期:[null,null],
状态:空,
投入:{
签入:$(“#签入”),
结帐:$(“#结帐”),
日期:$(“#日期”)
}
};
datepicker.container.datepicker({
月数:2,
dateFormat:datepicker.dateFormat,
minDate:0,
maxDate:null,
beforeShowDay:功能(日期){
var highlight=false,
currentTime=date.getTime(),
selectedTime=datepicker.dates;
//突出显示日期范围

如果((selectedTime[0]&&selectedTime[0]==currentTime)| |(selectedTime[1]&¤tTime>=selectedTime[0]&¤tTime这个答案对我很有帮助!非常感谢!还有一件事,你能帮我更新这个,让它延续到下个月吗?我在一个选取器中显示了两个月。谢谢!@Marvzz我添加了一个答案,其中包括一个脚本,可以用多个月的时间完成它。有人请将它迁移到jQuery 3。@AbhishekSaini应该在jQuery 3上工作,但没有经过测试。f这里的函数在jq3中被弃用了?不,不起作用。
live()
die()
不在jquery3中。
$("#input-service_date_leave, #input-service_date_return").datepicker({
    rangeSelect: true,
    beforeShow: customRange,
    onSelect: customRange,
    numberOfMonths: [1, 2],
});

function customRange(input) {
    if (input.id == "input-service_date_leave") {

        $("#ui-datepicker-div td").die();

        if (selectedDate != null) {
            $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
        }
    }
    if (input.id == "input-service_date_return") {

        $("#ui-datepicker-div td").live({
            mouseenter: function() {
                $(this).parent().addClass("finalRow");
                $(".finalRow").parents('.ui-datepicker-group-last').parent().find('.ui-datepicker-group-first').find('tr').last().addClass("finalRowRangeOtherTable");
                $(".finalRowRangeOtherTable").find("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                $(".finalRowRangeOtherTable").prevAll().find("td:not(.ui-datepicker-unselectable)").addClass("highlight");

                $(".finalRow").prevAll().find("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                $(this).prevAll("td:not(.ui-datepicker-unselectable)").addClass("highlight");
           },
            mouseleave: function() {
                $(this).parent().removeClass("finalRow");
                $("#ui-datepicker-div td").removeClass("highlight");

                  $(".finalRowRange").removeClass("finalRowRange").find('.highlight').removeClass("highlight");
            $(".finalRowRangeOtherTable").removeClass("finalRowRangeOtherTable").find('.highlight').removeClass("highlight");

            }
        });

        var selectedDate = $("#input-service_date_leave").datepicker("getDate");                
        if (selectedDate != null) {
            $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
        }
    }
}
$(function(){
    var datepicker = {
        container: $("#datepicker"),
        dateFormat: 'mm/dd/yy',
        dates: [null, null],
        status: null,
        inputs: {
            checkin: $('#checkin'),
            checkout: $('#checkout'),
            dates: $('#dates')
        }
    };

    datepicker.container.datepicker({
        numberOfMonths: 2,
        dateFormat: datepicker.dateFormat,
        minDate: 0,
        maxDate: null,

        beforeShowDay: function(date) {
            var highlight = false,
            currentTime = date.getTime(),
            selectedTime = datepicker.dates;

            // Highlight date range
            if ((selectedTime[0] && selectedTime[0] == currentTime) || (selectedTime[1] && (currentTime >= selectedTime[0] && currentTime <= selectedTime[1]))) highlight = true;

            return [true, highlight ? 'ui-datepicker-select' : ""];
        },
        onSelect: function(dateText) {

            if (!datepicker.dates[0] || datepicker.dates[1] !== null) {
                // CHOOSE FIRST DATE

                // fill dates array with first chosen date
                datepicker.dates[0] = $.datepicker.parseDate(datepicker.dateFormat, dateText).getTime();
                datepicker.dates[1] = null;

                // clear all inputs
                datepicker.inputs.checkin.val('');
                datepicker.inputs.checkout.val('');
                datepicker.inputs.dates.val('');

                // set current datepicker state
                datepicker.status = 'checkin-selected';

                // create mouseover for table cell
                $('#datepicker').delegate('.ui-datepicker td', 'mouseover', function(){

                    // if it doesn't have year data (old month or unselectable date)
                    if ($(this).data('year') == undefined) return;

                    // datepicker state is not in date range select, depart date wasn't chosen, or return date already chosen then exit
                    if (datepicker.status != 'checkin-selected') return;

                    // get date from hovered cell
                    var hoverDate = $(this).data('year')+'-'+($(this).data('month')+1)+'-'+$('a',this).html();

                    // parse hovered date into milliseconds
                    hoverDate = $.datepicker.parseDate('yy-mm-dd', hoverDate).getTime();

                    $('#datepicker td').each(function(){

                        // compare each table cell if it's date is in date range between selected date and hovered
                        if ($(this).data('year') == undefined) return;

                        var year = $(this).data('year'),
                            month = $(this).data('month'),
                            day = $('a', this).html();

                        var cellDate = $(this).data('year')+'-'+($(this).data('month')+1)+'-'+$('a',this).html();

                        // convert cell date into milliseconds for further comparison
                        cellDate = $.datepicker.parseDate('yy-mm-dd', cellDate).getTime();

                        if ( (cellDate >= datepicker.dates[0] && cellDate <= hoverDate) || (cellDate <= datepicker.dates[0] && cellDate >= hoverDate) ) {
                            $(this).addClass('ui-datepicker-hover');
                        } else {
                            $(this).removeClass('ui-datepicker-hover');
                        }

                    });
                });

            } else {
                // CHOOSE SECOND DATE

                // push second date into dates array
                datepicker.dates[1] = $.datepicker.parseDate(datepicker.dateFormat, dateText).getTime();

                // sort array dates
                datepicker.dates.sort();

                var checkInDate = $.datepicker.parseDate('@', datepicker.dates[0]);
                var checkOutDate = $.datepicker.parseDate('@', datepicker.dates[1]);

                datepicker.status = 'checkout-selected';

                //fill input fields

                datepicker.inputs.checkin.val($.datepicker.formatDate(datepicker.dateFormat, checkInDate));
                datepicker.inputs.checkout.val($.datepicker.formatDate(datepicker.dateFormat, checkOutDate)).change();

                datepicker.inputs.dates.val(datepicker.inputs.checkin.val() + ' - ' + datepicker.inputs.checkout.val());

            }
        }
    });
});