您可以在顯示之前操作datepicker
,但請記住,如果頁面上有其他日期選擇器,則需要重置,因爲只有1個實際日期選擇器<div>
。
我有created a demo這可能會做你想做的。希望能幫助到你。
HTML
Normal: <input type="text">
<p>No days: <input type="text" class="noDays"></p>
CSS
p {
position:absolute;
bottom:5px;
}
div.noDays table {
display:none;
}
的JavaScript(需要jQuery和jQueryUI的)
$('input').datepicker({
showButtonPanel: true,
changeMonth: true,
changeYear: true,
dateFormat:'MM yy',
beforeShow: function(input, inst) {
if ($(input).hasClass('noDays')) {
$('#ui-datepicker-div').addClass('noDays');
} else {
$('#ui-datepicker-div').removeClass('noDays');
$(this).datepicker('option', 'dateFormat', 'yy-mm-dd');
}
},
onClose: function(dateText, inst) {
if ($('#ui-datepicker-div').hasClass('noDays')) {
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));
}
}
});
感謝安迪,你的方法固定次e問題。 – benb 2011-04-28 16:21:05