簡體   English   中英

如何將jQuery日期選擇器轉換為僅選擇月份和年份?

[英]How to convert jQuery date picker to select only month and year?

如何將jQuery日期選擇器轉換為僅選擇月份和年份? 我嘗試使用日期格式,它工作,但也顯示日期。 我正在嘗試一種只選擇月份和年份的方法

我寫了代碼,

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
   $(function() {
       $( "#datepicker" ).datepicker({dateFormat: 'MM yy'});
   });
</script>
<input type="text" id="datepicker">

你的答案就在這里。

http://jsfiddle.net/bopperben/DBpJe/

$(function() {
$('.date-picker').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));
    }
});
});

從他的帖子到nitin的信用... jQuery UI DatePicker只顯示月份

這是我為創建下拉列表而不是使用datepicker所做的。 只需要jQuery。

HTML:

 <select class="form-control" id="yearMonthInput"></select>

javascript代碼:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    for (i = new Date().getFullYear() ; i > 2008; i--) {
        $.each(months, function (index, value) {
            $('#yearMonthInput').append($('<option />').val(index + "_" + i).html(value + " " + i));
        });                
    }

看起來像這樣:

在此輸入圖像描述

$(document).ready(function() {
    $('#txtDate').datepicker({
    changeMonth: true,
    changeYear: 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));
   }
 }
});

});​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM