jQuery UI 根据开始日期选择范围内的开始和结束日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17016598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:16:08  来源:igfitidea点击:

jQuery UI Picking a start and end date within range based on start date

jqueryjquery-uijquery-ui-datepicker

提问by Matt Coady

I'm building a date picker in jQuery ui. What I'm trying to do is set a range, so when they pick the first date the second date appears and gives a 30 day window. I tried this but it doesn't work (it lets the user pick 30 days from today, not 30 from the start date):

我正在 jQuery ui 中构建一个日期选择器。我想要做的是设置一个范围,所以当他们选择第一个日期时,第二个日期出现并给出一个 30 天的窗口。我试过这个,但它不起作用(它让用户从今天开始选择 30 天,而不是从开始日期开始 30 天):

var pickDate;
$( "#datepickerEnd" ).hide();
$( "#datepickerStart" ).datepicker();
$( "#datepickerStart" ).change(function(){
    var pickDate = $( "#datepickerStart" ).val();
    $( "#datepickerEnd" ).datepicker({ minDate: pickDate, maxDate: +30 });
    $( "#datepickerEnd" ).show();
})

Another issue I ran into is when I change datepickerStart it'll only set the start range once but not every time I change it. I have to refresh the page to lock in a new start date.

我遇到的另一个问题是,当我更改 datepickerStart 时,它只会设置一次开始范围,而不是每次更改时。我必须刷新页面以锁定新的开始日期。

回答by SirDerpington

Check this jsfiddlehere. It's a working example of your problem.

jsfiddle在这里检查。这是您的问题的一个有效示例。

HTML

HTML

<input type="text" id="dt1">
<input type="text" id="dt2">

JS

JS

$(document).ready(function () {
    $("#dt1").datepicker({
        dateFormat: "dd-M-yy",
        minDate: 0,
        onSelect: function () {
            var dt2 = $('#dt2');
            var startDate = $(this).datepicker('getDate');
            //add 30 days to selected date
            startDate.setDate(startDate.getDate() + 30);
            var minDate = $(this).datepicker('getDate');
            var dt2Date = dt2.datepicker('getDate');
            //difference in days. 86400 seconds in day, 1000 ms in second
            var dateDiff = (dt2Date - minDate)/(86400 * 1000);

            //dt2 not set or dt1 date is greater than dt2 date
            if (dt2Date == null || dateDiff < 0) {
                    dt2.datepicker('setDate', minDate);
            }
            //dt1 date is 30 days under dt2 date
            else if (dateDiff > 30){
                    dt2.datepicker('setDate', startDate);
            }
            //sets dt2 maxDate to the last day of 30 days window
            dt2.datepicker('option', 'maxDate', startDate);
            //first day which can be selected in dt2 is selected date in dt1
            dt2.datepicker('option', 'minDate', minDate);
        }
    });
    $('#dt2').datepicker({
        dateFormat: "dd-M-yy",
        minDate: 0
    });
});

As soderslatt already mentioned use the onSelectoption to set the dates. Other methods I used are:

正如 soderslatt 已经提到的,使用onSelect选项来设置日期。我使用的其他方法是:

I think they're all very self explanatory and the documentation helps you to understand how they work. If you want to set the date of the second datepicker to dt1's date + 1 day do the same as in this line:

我认为它们都是不言自明的,文档可以帮助您了解它们的工作原理。如果要将第二个日期选择器的日期设置为 dt1 的日期 + 1 天,请执行与此行相同的操作:

startDate.setDate(startDate.getDate() + 30);

But of course add 1 day and not 30.

但是当然要加 1 天而不是 30 天。

回答by anderssonola

Try using the ui onSelect callback instead of .change(), http://api.jqueryui.com/datepicker/#option-onSelect

尝试使用 ui onSelect 回调而不是 .change(),http://api.jqueryui.com/datepicker/#option-onSelect

on init:

在初始化:

var $datepickerStart = $("#datepickerStart");
$datepickerStart.datepicker({
    onSelect: function( selectedDate ) {
        $datepickerStart.datepicker( "option", "minDate", selectedDate );
    }
});

回答by Saurabh

$("#start_date").datepicker().on('changeDate', function(selected){
        startDate = new Date(selected.date.valueOf());
        $('#end_date').datepicker('setStartDate', startDate);
    });  

    $("#end_date").datepicker().on('changeDate', function(selected){
        startDate = new Date(selected.date.valueOf());
        $('#start_date').datepicker('setEndDate', startDate);
    });