easyui datebox 只選擇月份的方法
效果如下圖:


代碼如下:
<html >
<head>
<meta charset="utf-8">
<title>ECharts · Example</title>
<link href="../easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
<link href="../easyui/themes/icon.css" rel="stylesheet" type="text/css" />
<script src="../js/jquery.min.js" type="text/javascript"></script>
<script src="../easyui/jquery.easyui.min.js" type="text/javascript"></script>
<script src="../easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
</head>
<body>
<input id="date1"/>
<input id="date2" style="display: none"/>
<button id="kk" style="width: 120px;height: 30px;">點擊切換成月份</button>
<script>
$(function () {
dateboxDate('date1');
$("#kk").on("click",function(){
$('#date1').hide();
$('#date1').next().hide();
dateboxMonth('date2');
});
});
function dateboxDate(id){
var bb = $('#' + id);
bb.datebox({
value : '2017/04/23',
editable : false
});
}
dateboxMonth = function (id) {
var db = $('#' + id);
db.datebox({
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); //月份,這里不需要+1
db.datebox('hidePanel')//隱藏日期對象
.datebox('setValue', year + '-' + month); //設置日期的值
});
}, 0);
yearIpt.unbind();//解綁年份輸入框中任何事件
},
parser: function (s) {
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) { return d.getFullYear() + '-' + (d.getMonth() + 1);/*getMonth返回的是0開始的,忘記了。。已修正*/ }
});
var p = db.datebox('panel'), //日期選擇對象
tds = false, //日期選擇對象中月份
aToday = p.find('a.datebox-current'),
yearIpt = p.find('input.calendar-menu-year'),//年份輸入框
//顯示月份層的觸發控件
span = aToday.length ? p.find('div.calendar-title span') ://1.3.x版本
p.find('span.calendar-text'); //1.4.x版本
p.find('div.calendar-header').hide();
if (aToday.length) {//1.3.x版本,取消Today按鈕的click事件,重新綁定新事件設置日期框為今天,防止彈出日期選擇面板
aToday.unbind('click').click(function () {
var now=new Date();
db.datebox('hidePanel').datebox('setValue', now.getFullYear() + '-' + (now.getMonth() + 1));
});
}
}
</script>
</body>
</html>
