【问题标题】:Allow only a cetain day of the month in datepicker在日期选择器中只允许一个月中的某一天
【发布时间】:2016-11-15 21:29:54
【问题描述】:

我的要求有点奇怪,但我不能没有。 我有一个 jquery datepicker,用户应该能够选择任何年份、任何月份,但只有本月的第 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,
});

不走运.. 非常感谢任何帮助

【问题讨论】:

    标签: jquery datepicker jquery-ui-datepicker


    【解决方案1】:

    在我的 Rails 应用程序中,jQueryUI 的 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,
       });
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

      • @TonyVincent:- 我没明白。你的意思是你不想显示除 21 以外的其他日期?
      • @TonyVincent:- 如果是这样,那么你可以尝试这样的事情:jsfiddle.net/DBpJe/8759
      • 应显示从 1 到 30 的所有日期,但应在视图中禁用除第 21 天以外的所有其他日期
      • @TonyVincent:- 我已经为这两种情况添加了演示。在第一个演示中,除 21 之外的所有日子都被禁用。其次,我对第 21 天的月份进行了硬编码,因此用户只需选择月份和年份。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      相关资源
      最近更新 更多