Jquery 日期选择器选择的日期错误

selected date of Jquery datepicker is wrong

我刚刚将年份范围从 1960 更改为 2003

但我在选择日期时在文本框中显示了当前年份 例如,如果我选择 march-4-1960 但它显示 '03/04/2015'

代码:

$(function() {
$( "#txt1" ).datepicker(
    {

     changeMonth: true,
            changeYear: true,
            yearRange: '1960:2003'
    });
});

HTML:

<input type="text" id="txt1"/>

使用这个Demo here

   $(function() {
$( "#txt1" ).datepicker({    
            changeMonth: true,
            changeYear: true,
            yearRange: '1960:2003',
      onClose: function(dateText, inst) { 
         var startDate = new Date(dateText);


        var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
        var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
        $(this).datepicker('setDate', new Date(year, month, inst.selectedDay));
    }
    });
});

解决此问题的一种方法是设置 defaultDate。我相信这种方法更简洁。

Updated jsFiddle

JS:

$(function() {
    $( "#txt1" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: "1960:2003"
        defaultDate:"1994-01-01"
    });
});

Source

defaultDate jQuery UI documentation


Re-updated jsFiddle based on your comment.