首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将jQuery日期选择器转换为只选择月份和年份?

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

Stack Overflow用户
提问于 2013-12-27 13:57:00
回答 3查看 75.3K关注 0票数 11

如何将jQuery日期选择器转换为仅选择月份和年份?我尝试使用日期格式,它的工作,但显示日期也。我正在尝试一种只选择月份和年份的方法

我写了代码,

代码语言:javascript
复制
<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">
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-12-27 14:04:09

你的答案就在这里。

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

代码语言:javascript
复制
$(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 to show month year only

票数 24
EN

Stack Overflow用户

发布于 2014-10-22 23:49:02

这就是我为创建下拉菜单而不是使用datepicker所做的。只需要jQuery。

HTML:

代码语言:javascript
复制
 <select class="form-control" id="yearMonthInput"></select>

javascript代码:

代码语言: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));
        });                
    }

看起来像这样:

票数 14
EN

Stack Overflow用户

发布于 2013-12-27 14:15:56

代码语言:javascript
复制
$(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));
   }
 }
});

});​
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20795597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档