我使用jQuery日期选择器在我的应用程序上显示日历。我想知道我是否可以使用它来显示月和年(2010年5月)而不是日历?


当前回答

我对公认的答案有一定的困难,没有其他答案可以用最小的努力作为基础。因此,我决定对已接受答案的最新版本进行调整,直到它至少满足最低的JS编码/可重用性标准。

这里有一个比第三版(最新版)本·克勒的公认答案更清晰的解决方案。此外,它将:

不仅要使用mm/yy格式,还要使用包括 OP的MM yy。 不隐藏页面上其他日期选择器的日历。 不会用datestr隐式污染全局JS对象, 月、年等变量。

看看吧:

$('.date-picker').datepicker({
    dateFormat: 'MM yy',
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    onClose: function (dateText, inst) {
        var isDonePressed = inst.dpDiv.find('.ui-datepicker-close').hasClass('ui-state-hover');
        if (!isDonePressed)
            return;

        var month = inst.dpDiv.find('.ui-datepicker-month').find(':selected').val(),
            year = inst.dpDiv.find('.ui-datepicker-year').find(':selected').val();

        $(this).datepicker('setDate', new Date(year, month, 1)).change();
        $('.date-picker').focusout();
    },
    beforeShow: function (input, inst) {
        var $this = $(this),
            // For the simplicity we suppose the dateFormat will be always without the day part, so we
            // manually add it since the $.datepicker.parseDate will throw if the date string doesn't contain the day part
            dateFormat = 'd ' + $this.datepicker('option', 'dateFormat'),
            date;

        try {
            date = $.datepicker.parseDate(dateFormat, '1 ' + $this.val());
        } catch (ex) {
            return;
        }

        $this.datepicker('option', 'defaultDate', date);
        $this.datepicker('setDate', date);

        inst.dpDiv.addClass('datepicker-month-year');
    }
});

你需要的其他所有东西是下面的CSS:

.datepicker-month-year .ui-datepicker-calendar {
    display: none;
}

就是这样。希望以上内容能节省更多读者的时间。

其他回答

再添加一个简单的解决方案

 $(function() {
    $('.monthYearPicker').datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'M yy'
    }).focus(function() {
        var thisCalendar = $(this);
        $('.ui-datepicker-calendar').detach();
        $('.ui-datepicker-close').click(function() {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
thisCalendar.datepicker('setDate', new Date(year, month, 1));
        });
    });
});

http://jsfiddle.net/tmnasim/JLydp/ 特点:

只显示月/年 仅在单击完成按钮时将月年值添加到输入框 当点击“完成”时,没有“重新打开”行为 ------------------------------------ 另一个解决方案,工作很好,日期和月在同一页:(也避免了在IE中多次点击前一个按钮的错误,这可能发生,如果我们使用焦点功能) JS小提琴link

在jQueryUI.com上搜索datepicker后,以下是我对你问题的结论和答案。

首先,我想对你的问题说不。你不能只使用jQueryUI datepicker来选择月份和年份。不支持。它没有回调函数。

但是你可以通过使用css来隐藏日期等,让它只显示月份和年份。我认为这没有意义因为你需要点击日期来选择日期。

我只能说你得用另一个约会工具。就像罗杰建议的那样。

使用onSelect回调并手动删除年份部分,并手动设置字段中的文本

感谢Ben Koehler的解决方案。

然而,我有一个问题与多个实例的日期选择,其中一些需要与日期选择。Ben Koehler的解决方案(在编辑3中)有效,但在所有情况下都隐藏了日期选择。下面是解决这个问题的更新:

$('.date-picker').datepicker({
    dateFormat: "mm/yy",
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    onClose: function(dateText, inst) {
        if($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1) {
            $(this).datepicker(
                'setDate',
                new Date(
                    $("#ui-datepicker-div .ui-datepicker-year :selected").val(),
                    $("#ui-datepicker-div .ui-datepicker-month :selected").val(),
                    1
                )
            ).trigger('change');
            $('.date-picker').focusout();
        }
        $("#ui-datepicker-div").removeClass("month_year_datepicker");
    },
    beforeShow : function(input, inst) {
        if((datestr = $(this).val()).length > 0) {
            year = datestr.substring(datestr.length-4, datestr.length);
            month = datestr.substring(0, 2);
            $(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
            $(this).datepicker('setDate', new Date(year, month-1, 1));
            $("#ui-datepicker-div").addClass("month_year_datepicker");
        }
    }
});

我尝试了这里提供的各种解决方案,如果您只是想要几个下拉列表,它们工作得很好。

最好的(外表等)这里建议的'picker' (https://github.com/thebrowser/jquery.ui.monthpicker)基本上是旧版jquery-ui datepicker的副本,并重写了_generateHTML。然而,我发现它不再与当前的jquery-ui(1.10.2)很好地发挥,并有其他问题(esc不关闭,在其他窗口小部件打开时不关闭,有硬编码的样式)。

我没有试图修复那个月份选择器,也没有用最新的日期选择器重新尝试相同的过程,而是钩入了现有日期选择器的相关部分。

这包括重写:

_generateHTML(构建月份选择器标记) parseDate(因为它不喜欢没有day组件), _selectDay (datepicker使用.html()获取日期值)

由于这个问题有点老,并且已经得到了很好的回答,这里只有_selectDay覆盖来展示这是如何完成的:

jQuery.datepicker._base_parseDate = jQuery.datepicker._base_parseDate || jQuery.datepicker.parseDate;
jQuery.datepicker.parseDate = function (format, value, settings) {
    if (format != "M y") return jQuery.datepicker._hvnbase_parseDate(format, value, settings);
    // "M y" on parse gives error as doesn't have a day value, so 'hack' it by simply adding a day component
    return jQuery.datepicker._hvnbase_parseDate("d " + format, "1 " + value, settings);
};

如上所述,这是一个老问题,但我发现它有用,所以想添加反馈与替代解决方案。