0
var dates = $("#filterDateStart, #filterDateEnd").datepicker({
        dateFormat: "yy-mm-dd",
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function (selectedDate) {
            var option = this.id == "filterDateStart" ? "minDate" : "maxDate",

                    instance = $(this).data("datepicker"),
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings);
            dates.not(this).datepicker("option", option, date);
        }
    });
4

1 回答 1

1

这应该工作

$("#filterDateStart").datepicker({
    onSelect: function () {
       var dateObj = $(this).datepicker( 'getDate' );
       $("#filterDateEnd").datepicker( "option", "defaultDate", dateObj);
    }
});

编辑

用于测试的 HTML:

<form id="testForm" action="#">
<p>Date: <input type="text" id="filterDateStart" /></p>
<p>Date: <input type="text" id="filterDateEnd" /></p>

<input type="submit" value="check end date default "/>
</form>

JavaScript

$(document).ready(function(){
    $("#filterDateStart").datepicker({
        dateFormat: "yy-mm-dd",
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function () {
           var startDate = $(this).datepicker( 'getDate');
           var endDate = $("#filterDateEnd").datepicker( 'getDate' );
           if($("#filterDateEnd").datepicker( 'getDate' ) == null || startDate > endDate)
           {
               $("#filterDateEnd").datepicker( "setDate", startDate );
           }
        }
    });
    $("#filterDateEnd").datepicker({
        dateFormat: "yy-mm-dd",
        changeMonth: true,
        numberOfMonths: 1
    });

    $("#testForm").submit(function(e){
        e.preventDefault(); 
        alert( $("#filterDateEnd").datepicker("getDate"));
    })
});

编辑2: 工作小提琴

于 2013-09-16T12:12:09.237 回答