一、效果图
二、引入easyui(为了方便引入了在线版)
<link href="https://cdn.insdep.com/themes/1.0.0/easyui.css" rel="stylesheet" type="text/css">
<link href="https://cdn.insdep.com/themes/1.0.0/easyui_animation.css" rel="stylesheet" type="text/css">
<link href="https://cdn.insdep.com/themes/1.0.0/easyui_plus.css" rel="stylesheet" type="text/css">
<link href="https://cdn.insdep.com/themes/1.0.0/insdep_theme_default.css" rel="stylesheet" type="text/css">
<link href="https://cdn.insdep.com/themes/1.0.0/icon.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="https://cdn.insdep.com/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://cdn.insdep.com/easyui/jquery.easyui-1.5.1.min.js"></script>
<script type="text/javascript" src="https://cdn.insdep.com/themes/1.0.0/jquery.insdep-extend.min.js"></script>
三、页面代码如下
<body style="text-align: center">
<input id="month"/>
</body>
<script type="text/javascript">
$("#month").datebox({
height: 26,
width: 130,
editable: false,
onShowPanel: function () {// 显示日趋选择对象后再触发弹出月份层的事件,初始化时没有生成月份层
span.trigger('click'); // 触发click事件弹出月份层
if (!tds)
setTimeout(function() { //延时触发获取月份对象,因为上面的事件触发和对象生成有时间间隔
tds = p.find('div.calendar-menu-month-inner td');
tds.click(function(e) {
e.stopPropagation(); //禁止冒泡执行easyui给月份绑定的事件
var year = /\d{4}/.exec(span.html())[0] //得到年份
,month = parseInt($(this).attr('abbr'), 10); //月份 之前是这样的month = parseInt($(this).attr('abbr'), 10) + 1;
$('#month').datebox('hidePanel') //隐藏日期对象
.datebox('setValue', year + '-' + month); //设置日期的值
});
}, 0);
},
parser: function (s) {//配置parser,返回选择的日期
if (!s) return new Date();
var arr = s.split('-');
return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
},
formatter: function (d) {
if (d.getMonth() == 0) {
return d.getFullYear() - 1 + '-' + 12;
} else {
var tmp_month = d.getMonth()+1;
if (tmp_month < 10) {
tmp_month = '0' + tmp_month;
}
return d.getFullYear() + '-' + tmp_month;
}
},// 配置formatter,只返回年月
onChange: function (newDate, oldDate) {
var arr = newDate.split('-');
var tmp_year = parseInt(arr[0], 10);
var tmp_month = parseInt(arr[1], 10);
if (tmp_month < 10) {
tmp_month = '0' + tmp_month;
}
m_date = tmp_year + '-' + tmp_month;
},
});
var p = $('#month').datebox('panel'), // 日期选择对象
tds = false, // 日期选择对象中月份
span = p.find('span.calendar-text'); // 显示月份层的触发控件
$("#month").datebox("setValue",getYear()+"-"+getMonth());
function getYear(){
var myDate = new Date();
var year = managerNum(myDate.getFullYear()); //获取完整的年份(4位,1970-????)
return year;
}
function getMonth(){
var myDate = new Date();
var month = managerNum(myDate.getMonth()+1); //获取当前月份(0-11,0代表1月)
return month;
}
function managerNum(data){
if(parseInt(data)<10){
return "0"+data;
}else{
return "" +data;
}
}
</script>