【问题标题】:two date pickers one showing month year only两个日期选择器一个仅显示月份年份
【发布时间】:2017-03-10 04:52:06
【问题描述】:

$(".datepicker").datepicker({
  changeMonth: true,
  changeYear: true,
  dateFormat: 'yy-mm-dd',
  yearRange: '1800:2100',
  onSelect: function(value, ui) {


  }
});
$('.yearmonth').datepicker({
  changeMonth: true,
  changeYear: true,
  showButtonPanel: true,
  dateFormat: 'yy-mm',
  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));
  }

});
.yearmonth .ui-datepicker-calendar {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
Year and Month : <input class="yearmonth" type="text"  /><br>
Complete Date : <input class="datepicker" type="text"  />

我怎样才能在一个上下文中拥有 2 个日期选择器,它有一个完整的日期,而另一个只有月份和年份?

尝试使用 CSS 隐藏,但我无法确定如何选择仅适用于月份和年份的日期选择器

我想要的是当我点击年月时日历应该被隐藏。

【问题讨论】:

标签: jquery css datepicker


【解决方案1】:

使用下面的代码。根据您的要求更改dateFormat: 'MM yy'。

$(function() {
    $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        onClose: function(dateText, inst) { 
            $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
        }
    });
});
.ui-datepicker-calendar {
    display: none;
    }
<!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.1/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css">
<body>
    <label for="startDate">Date :</label>
    <input name="startDate" id="startDate" class="date-picker" />
</body>

【讨论】:

    【解决方案2】:

    更改格式 dateFormat: 'yy-mm', or 'yy-mm-dd',

    $(".datepicker").datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: 'yy-mm',
      yearRange: '1800:2100',
      onSelect: function(value, ui) {
    
    
      }
    });
    $('.yearmonth').datepicker({
      changeMonth: true,
      changeYear: true,
      showButtonPanel: true,
      dateFormat: 'yy-mm',
      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));
      }
    
    });
    .yearmonth .ui-datepicker-calendar {
      display: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
    Year and Month : <input class="yearmonth" type="text"  /><br>
    Complete Date : <input class="datepicker" type="text"  />

    【讨论】:

      【解决方案3】:

      要在单击月份或年份后隐藏日历,您需要添加 日期选择器中的 onChangeMonthYear 事件函数。

      试试这个

      $("#datep").datepicker({
          changeMonth: true,
          changeYear: true,
         onChangeMonthYear: function(year, month, widget) {
              setTimeout(function() {
                 $('.ui-datepicker-calendar').hide();
              });
          },
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-02
        • 2012-02-22
        • 2015-02-26
        • 1970-01-01
        相关资源
        最近更新 更多