简体   繁体   English

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

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

How to convert jQuery date picker to select month and year only?. 如何将jQuery日期选择器转换为仅选择月份和年份? I tried it using date format, it working but show dates too. 我尝试使用日期格式,它工作,但也显示日期。 I am trying a way to select month and year only 我正在尝试一种只选择月份和年份的方法

I wrote code, 我写了代码,

<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">

Your answer is here. 你的答案就在这里。

http://jsfiddle.net/bopperben/DBpJe/ 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));
    }
});
});

Credits to nitin from his post... jQuery UI DatePicker to show month year only 从他的帖子到nitin的信用... jQuery UI DatePicker只显示月份

This is what I did for create a dropdown instead of use datepicker. 这是我为创建下拉列表而不是使用datepicker所做的。 Just jQuery is needed. 只需要jQuery。

HTML: HTML:

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

javascript code: 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));
        });                
    }

And looks in this way: 看起来像这样:

在此输入图像描述

$(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.

相关问题 EXTJS 5 - 仅限日期选择器年份和月份 - EXTJS 5 - Date picker year and month only jQuery日期选择器首先选择年份然后选择月份 - jQuery Date Picker Pick Year First and then Month jQuery UI datepicker功能仅选择年份或年份-月份或年份-月份-日期 - jQuery UI datepicker functionality to select only year or year-month or year-month-date 如何转换此日期范围(月,年)日期选择器以数字格式显示月份的输出 - How do I convert this date range (month,year) date picker to show the output of the month in numeric format 我想制作只有年份和月份选择器的日期范围选择器 - I want to make date range picker with only Year and month picker 更新月/年更改中的日期字段-jQuery日期选择器 - Update The Date Field on Change in month/Year -Jquery Date Picker 克隆年份和月份中的jQuery日期选择器不起作用,默认日期未显示 - jquery date picker in clone year and month not working and default date was not showing jQuery日期选择器选择突出显示的月份 - Jquery date picker select highlighted month 在jQuery中将年,月和日转换为单个日期 - Convert year, month and day to single date in jQuery 如何提供一个日期选择器,其中包含选择日、月和年的选项 - how to give a date picker which consists of an option to select for day,month and year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM