Datepicker月份和年份选择错误

时间:2017-09-18 15:58:48

标签: javascript jquery jquery-ui datepicker jquery-ui-datepicker

我想构建一个只接受Month和Year的日期选择器,所以我只需使用display:none来隐藏CSS的日子,这对我来说很好。 然后我更改了datepicker选项,用户只选择Month和Year,并且工作得很好。

例如,如果我选择" JAN / 2017" ,则输入变化很好。 但是,如果再次点击输入,它将不是 2017年1月,将 2017年9月。为什么呢?

这是一个掠夺者:CLICK HERE =)

如果您希望查看代码,请执行以下操作:

  <body>
<style>
  .ui-datepicker-calendar {display: none!important;}
</style>

<input class="date-picker">

<script>
$('document').ready(function () {
        $('.date-picker').datepicker({
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            dateFormat: 'MM yy',
            onClose: function (dateText, inst) {
                $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
            }
        });

    })
</script>

非常感谢!

@EDIT - 已解决!

我不知道这是不是最好的方式,但是有效! 我只是制作一些操纵DOM的事件

        var mmNow, yyNow;

    $('document').ready(function () {
        $('.date-picker').datepicker({
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            dateFormat: 'MM yy',
            onClose: function (dateText, inst) {
                $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
            }
        }).click(function () {
            $('.date-picker').datepicker('setDate', new Date(yyNow, mmNow, 1));
        }).blur(function(){
            mmNow = ($('.ui-datepicker-month')[0].value)
            yyNow = ($('.ui-datepicker-year')[0].value)
        })
    })

2 个答案:

答案 0 :(得分:0)

如果您想要展示,例如,2017年9月&#39;在您的输入中,如果您想要斜杠,则应将dateFormat更改为M yy或更改为M/yy

答案 1 :(得分:0)

如果你使用它,它应该解决你的问题,除了它也会显示日期

dateFormat: 'MM/dd/yy',