Jquery datepicker下拉列表显示当前日期而不是所选日期

问题描述 投票:2回答:2

如果你看,开始日期的选定值是'2016年12月',但当我点击它时,它显示当前的月份和年份。它应显示所选值(2016年12月)。知道如何解决它?

以下是开始日期datepicker的代码:

$('#startDateEdEdit').datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        setDate: startDateEd,
        yearRange: `1920:${todayYear+10}`,
        onClose: function(dateText, inst) {
            startDateEdEdit = new Date(inst.selectedYear, inst.selectedMonth, 1);
            $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
            if(endDateEdEdit < startDateEdEdit){
                $('#endDateEdEdit').datepicker('setDate',null);
            }
            else {
                var duration = $('#endDateEdEdit').datepicker('getDate')?durationCalculator(startDateEdEdit, endDateEdEdit):null;
                document.getElementById('durationEdit').value = duration.duration;
                document.getElementById('eduEdit-duration').value = duration.years;
            }
        }
    });
javascript jquery date datepicker
2个回答
0
投票

datepicker dateformat字段似乎存在一些问题。你可以做的是用dateFormat: 'MM yy'代替dateFormat: 'dd MM, yy'取代那会有用吗?


0
投票

您需要在onClose和beforeShow事件之间进行调整。

$(function() {
$('.date-picker').datepicker( {
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: 'MM yy'
 onClose: function() {
    var iMonth = $("#ui-datepicker-div .ui-datepicker-month :selected").val();

    var iYear = $("#ui-datepicker-div .ui-datepicker-year :selected").val();

    $(this).datepicker('setDate', new Date(iYear, iMonth, 1));

 },
 beforeShow: function() {

   if ((selDate = $(this).val()).length > 0)

   {
      iYear = selDate.substring(selDate.length - 4, selDate.length);
      iMonth = jQuery.inArray(selDate.substring(0, selDate.length - 5),
               $(this).datepicker('option', 'monthNames'));

      $(this).datepicker('option', 'defaultDate', new Date(iYear, iMonth, 1));
      $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
   }
}
});});

JSFiddle

© www.soinside.com 2019 - 2024. All rights reserved.