JQuery实现先择年份和月份,不要日

来源:互联网 发布:java测试代码是什么 编辑:程序博客网 时间:2024/05/24 06:33

首先,要有以下几个文件

<link href="include/jquery-ui-1.7.3.custom.css" type="text/css" rel="Stylesheet"/>
<SCRIPT Language="JavaScript" src="include/jquery-1.3.2.min.js"></SCRIPT>
<SCRIPT Language="JavaScript" src="include/jquery-ui-1.7.3.custom.min.js"></SCRIPT>
<SCRIPT Language="JavaScript" src="include/ui.datepicker-zh-CN.js"></SCRIPT>

然后,就可以开始写初始化代码了,

<script type="text/javascript">
jQuery(function() {
jQuery('.date-picker').datepicker( {
showMonthAfterYear: true,
changeYear: true,
changeMonth: true,
showButtonPanel: false,
dateFormat: 'yy-mm',
nextText: '下个月',
prevText: '上个月',
hideIfNoPrevNext: false,
onClose: function(dateText, inst) {
var month = jQuery("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = jQuery("#ui-datepicker-div .ui-datepicker-year :selected").val();
jQuery(this).datepicker('setDate', new Date(year, month, 1));
document.getElementById("actDate").value = year + '-' + (parseInt(month)+parseInt(1)) + '-' + '01';
},
beforeShow : function(input, inst) {
if ((datestr = jQuery(this).val()).length > 0) {
year = datestr.substring(0,4);
month = datestr.substring(5);
jQuery(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
jQuery(this).datepicker('setDate', new Date(year, month-1, 1));
}
}
});
});
</script>

以下css用于屏蔽日的选择界面,而只有年份和月份,

<style>
.ui-datepicker-calendar
{
display: none;
}
</style>