两个日期选择器只显示一个月的年份

时间:2017-03-10 04:52:06

标签: jquery css datepicker



$(".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"  />
&#13;
&#13;
&#13;

如何在一个具有完整日期的上下文中使用2个datepicker而另一个只有月份和年份?

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

  

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

3 个答案:

答案 0 :(得分:1)

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

&#13;
&#13;
$(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));
        }
    });
});
&#13;
.ui-datepicker-calendar {
    display: none;
    }
&#13;
<!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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

更改格式dateFormat:'yy-mm'或'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"  />

答案 2 :(得分:0)

  

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

尝试一下

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