2012-01-30 279 views
3

我想顯示一個datepicker只顯示像這個Q'jquery date picker to show month year only這樣的月份+年菜單,這不是一個問題,但我有問題以下如何設置datepicker的日期只顯示月份和年份

我想輸入文本加載日期的字符串值,如01/2011,我希望在focusin事件datepicker將加載顯示的日期,我的意思是我希望它打開與2011年和01月已經選定,

但由於某些原因,當我點擊文本輸入原始日期「01/2011」更改爲「01/01/2011」

這裏是我的代碼領先

  $(document).delegate("#timeSpanFrom, #timeSpanTo", "focusin", function() { 
      var $this = $(this); 
      var inputDate = $.datepicker.parseDate('dd/mm/yy', "01/"+$this.val()); 
      if (!$this.data('datepicker_enabled')) { 
       $this.datepicker({ 
        changeMonth : true, 
        changeYear : true, 
        showButtonPanel : true, 
        onClose : function(dateText, inst) { 
         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, 1)); 
         $(".ui-datepicker-calendar").hide(); 
        } 
       }); 
       $this.datepicker({ dateFormat: 'mm/yy' }); 
       $this.datepicker('setDate', inputDate); 
       $this.data('datepicker_enabled',true); 
      } 
     }); 

     $(document).delegate("#timeSpanFrom, #timeSpanTo", "focus", function() { 
      $(".ui-datepicker-calendar").hide(); 
     });  

感謝

+0

則可以創建一個的jsfiddle? – 2012-01-30 17:24:22

+0

那麼,您是否希望datepick在彈出窗口中顯示月份和年份,或者與當前月份中的日期一起顯示? – Cheery 2012-01-30 18:12:53

+0

@SérgioMichels:不能做jsfiddle atm:/ @Cheery:我很喜歡彈出窗口(隱藏日子)我希望輸入字段只顯示月/年而不是日/月/年輸入字段以及從日期選擇器中選擇日期 – Daniel 2012-01-30 18:26:47

回答

6
<style> 
.ui-datepicker-calendar { 
    display: none; 
} 
</style> 
<script> 
    $(document).ready(function(){ 
    $('#timeSpanFrom, #timeSpanTo').datepicker({ 
     changeMonth: true, 
     changeYear: true, 
     showButtonPanel: true, 
     dateFormat: 'mm/yy', 
     onClose: function(dateText, inst) { 
      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, 1)); 
     }, 
     beforeShow : function(input, inst) { 
      var tmp = $(this).val().split('/'); 
      $(this).datepicker('option','defaultDate',new Date(tmp[1],tmp[0]-1,1)); 
      $(this).datepicker('setDate', new Date(tmp[1], tmp[0]-1, 1)); 
     } 
    }); 
}); 
</script> 

這裏一個更通用的解決方案jsfiddle

+0

Thx,但它似乎只在一個方向工作http:///jsfiddle.net/TXm9e/從日期選擇器中選擇日期並再次打開日期選擇器後,先前選擇的值無效(原始日期正在日期選擇器中顯示) – Daniel 2012-01-30 18:52:08

+0

@Daniel對不起,請稍等要做的事情。檢查代碼的更新版本。最好是使輸入字段只讀:) – Cheery 2012-01-30 20:48:07

+0

再次感謝,看起來是正確的方式,任何想法爲什麼年份下拉菜單隻包含2011年突然? http://jsfiddle.net/XsRUF/? – Daniel 2012-01-30 21:08:50

相關問題