JQuery日期时间选择器 - 只需选择月份和年份

Dar*_*ren 10 jquery datetime datepicker

我通过这个帖子jQuery UI DatePicker只显示月份,这对我来说非常有帮助,以便破解日期时间选择器UI以适应我的情况.

但是由于我在同一个表单上有几个日期时间选择器,所以会发生这种情况.css display:none旨在"隐藏"不显示的日子.但是如果我在同一个表单上有几个日期时间选择器,但只有一个我想制作一个月份选择器,我该如何实现呢?使用thw css,它会使所有日期时间日历UI的所有日子都消失,因为css将更改.ui-datepicker-calendar的属性.欢迎任何建议.

Ben*_*ler 9

目前,datepicker每页只创建一个所有'datepicker'输入共享的元素,因此为什么链接问题的答案不起作用.beforeShow事件不允许使用.addClass()或.css()编辑datepicker元素(因为元素结构在显示时会从datepicker脚本中刷新.)

为了解决这个问题,我发现输入元素的焦点事件似乎在显示日期选择器之后运行代码.这是一个令人沮丧的问题的简单解决方法.

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css">
<script type="text/javascript">
$(function() {
    $('.month-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));
        },
        beforeShow : function(input, inst) {
            if ((datestr = $(this).val()).length > 0) {
                year = datestr.substring(datestr.length-4, datestr.length);
                month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker('option', 'monthNames'));
                $(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
                $(this).datepicker('setDate', new Date(year, month, 1));
            }
        }
    });
    $('.date-picker').datepicker();
    $('.month-picker').focus(function () {
        $('.ui-datepicker-calendar').addClass('hideDates');
    });
});
</script>
<style>
.hideDates {
    display: none;
}
</style>
</head>
<body>
    <label for="startDate">Date:</label>
    <input name="startDate" id="startDate" class="date-picker" />
    <label for="endMonth">End Month:</label>
    <input name="endMonth" id="endMonth" class="month-picker" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)


Pad*_*ddy 1

我做了类似的事情,但制作了一个单独的“版本”的 datepicker 脚本,并将其命名为 Monthpicker,拥有自己的 CSS 选择器和 CSS 文件。这意味着您可以在同一页面上使用其中一个或两个。