3

下面的片段属于选择两个日期,我必须为此添加另一个功能,即结束日期选择应限制为比开始日期大一天

$(function() {
    $( "#fromDate" ).datepicker({
        defaultDate: "+0",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat:"yy-mm-dd",
        onClose: function( selectedDate ) {
            $( "#toDate" ).datepicker( "option", "minDate", selectedDate);
            this.focus();
        }
    });
    $( "#toDate" ).datepicker({
        defaultDate: "+0",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat:"yy-mm-dd",
        onClose: function( selectedDate ) {
            $( "#fromDate" ).datepicker( "option", "maxDate", selectedDate );
        }
    });
});

提前谢谢!!!

4

3 回答 3

0

我不知道这是正确的方法,但它会根据您的需要工作

$(function() {
        $( "#from" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onClose: function( selectedDate ) {
                var today = new Date(selectedDate);
                var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
                var curr_date = tomorrow.getDate();
                var curr_month = tomorrow.getMonth() + 1; //Months are zero based
                var curr_year = tomorrow.getFullYear();

                var max_string = curr_month+"/"+curr_date+"/"+curr_year;

                $( "#to" ).datepicker( "option", "minDate", selectedDate );
                $( "#to" ).datepicker( "option", "maxDate", max_string );
            }
        });
        $( "#to" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onClose: function( selectedDate ) {
                $( "#from" ).datepicker( "option", "maxDate", selectedDate );
            }
        });
    });
于 2012-12-19T06:43:31.160 回答
0

简单的解决你的问题,就两行js代码和BINGO!!。只需根据要求更改数字(如果您想将 Max days 设置为 1 或更多)

$("#from").datepicker({
                defaultDate: "+1w",
                changeMonth: true,
                numberOfMonths: 1,
                onClose: function (selectedDate) {
                    var d = new Date(selectedDate);
                    d.setDate(d.getDate() + 1);
                    $("#to").datepicker("option", "minDate", selectedDate);
                    $("#to").datepicker("option", "maxDate", d);
                }
            });
            $("#to").datepicker({
                defaultDate: "+1w",
                changeMonth: true,
                numberOfMonths: 1,
                onClose: function (selectedDate) {
                    $("#from").datepicker("option", "maxDate", selectedDate);
                }
            });
于 2012-12-19T07:35:56.210 回答
0
$(document).ready(function(){
    $("#txtFromDate").datepicker({ 
        numberOfMonths: 2,
        onSelect: function(selected) { 
            $("#txtToDate").datepicker("option","minDate", selected) 
        }
    });
    $("#txtToDate").datepicker({
        numberOfMonths: 2,
        onSelect: function(selected) { 
            $("#txtFromDate").datepicker("option","maxDate", selected)
        }
    });
});
于 2014-02-04T12:14:46.653 回答