2017-09-18 121 views
0

我想構建一個只接受月份和年份的日期選擇器,所以我簡單地使用display:none隱藏CSS日子,這對我來說很好。 然後我改變日期選擇器選項,用戶只選擇月和年,並且它的工作很好。日期選擇器月份和年份選定的錯誤

例如,如果我選擇「JAN/2017」,則輸入變化良好。 但是,如果您再次點擊輸入,它將不會是JAN/2017,將是SEP 2017 2017。爲什麼?

這裏是一個plunker: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) 
     }) 
    }) 

回答

0

如果你想顯示例如「2017年9月」在你輸入你應該,如果你想在你的日期格式更改爲「M YY」或「M /日」削減。

+0

我想我是不太清楚......嘗試把月/ 2017年和確認...然後點擊輸入再次...您將看到輸入將爲2017年9月,而不是2017年1月。這是我的問題。 = / – Johnson

0

如果你使用這個應該解決您的問題,但它會顯示日期,以及

dateFormat: 'MM/dd/yy', 
相關問題