在日期选择器中只允许一个月中的某一天

Allow only a cetain day of the month in datepicker

我的要求有点奇怪,但我不能没有。 我有一个 jquery 日期选择器,用户应该能够 select 任何一年,任何月份,但只能是每月的第 21 天。我试过了

$( "#t_funin_teate_shikyu_change_dt" ).datepicker({
    beforeShowDay: function(date){
      var dt = date.getDate();
      return [dt == 21, ""];
    },
    language: "ja",
    orientation: "bottom auto",
    toggleActive: true,
    autoclose: true,
});

运气不好..非常感谢任何帮助

试试这个:

$(function(){
    $("input").datepicker(
        {
        beforeShowDay: function (date) {
        if (date.getDate() == 21 ) {
            return [true, ''];
        }
        return [false, ''];
        }
        }

    );
});

JSFIDDLE DEMO

编辑:

如果您不想看到 21 以外的日期并对其进行硬编码,那么您可以这样尝试

$(function() {
    $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'mm/dd/yy',
        onClose: function(dateText, inst) { 
            var day = 21;
            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, day));
        }
    });
});

DEMO

在我的 rails 应用程序中,jQuery UI 的 beforeShowDay 方法不起作用,我仍然不明白为什么!对我有用的在下面

$( "#t_funin_teate_shikyu_change_dt" ).datepicker({
    format: "yyyy/mm/21",
    startView: "months",
    minViewMode: "months",
    language: "ja",
    orientation: "bottom auto",
    toggleActive: true,
    autoclose: true,
   });