如何将jQuery日期选择器转换为仅选择月份和年份?

use*_*298 11 javascript jquery datepicker

如何将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">
Run Code Online (Sandbox Code Playgroud)

小智 24

你的答案就在这里.

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));
    }
});
});
Run Code Online (Sandbox Code Playgroud)

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

  • 这只是来自nitin的复制和粘贴提供链接,请参阅http://stackoverflow.com/questions/2208480/jquery-ui-datepicker-to-show-month-year-only (9认同)

Dan*_*lva 13

这是我为创建下拉列表而不是使用datepicker所做的.只需要jQuery.

HTML:

 <select class="form-control" id="yearMonthInput"></select>
Run Code Online (Sandbox Code Playgroud)

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));
        });                
    }
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

在此输入图像描述