使用jquery中的开始日期限制结束日期

时间:2012-12-19 06:39:39

标签: javascript jquery

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

$(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 );
        }
    });
});
事先提前!!!

3 个答案:

答案 0 :(得分: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 );
            }
        });
    });

答案 1 :(得分:0)

简单解决您的问题,只需两行js代码和BINGO !! 只需根据要求更改数量(如果您想将Max天数设置为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);
                }
            });

答案 2 :(得分: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)
        }
    });
});