在需要選擇日期的表單中,一個好用的日曆控件能大幅提升使用者體驗。這個控件的效果是:點擊文字框自動彈出日曆,點擊目標日期後自動填入,操作直觀又便利。
點擊這裡查看效果:https:///keleyi/phtml/jstexiao/10.htm
<table width="300" align="center" > <tr> <td height="100" valign="middle"><strong>日期選擇控件:</strong></td> <td width="30"></td> <td valign="middle"><input id="calendar" type="text"></td> </tr> </table>
<style>
div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td, em, button {
margin: 0;
padding: 0;
}
body {
color: #333333;
font-family: Simsun,sans-serif;
font-size: 12px;
line-height: 1.5;
}
</style>
<script src="cQuery_110421.js" charset="utf-8" type="text/javascript"></script>
<script src="js/calendar.js" charset="utf-8" type="text/javascript"></script>
<script type="text/javascript">
$("#calendar").regMod("calendar","6.0", {
options: {
autoShow: !1,
showWeek: !0,
maxDate: function() {
var a = (new Date).addYears(1);
return a.getFullYear() + "-" + (a.getMonth() + 1) + "-" + a.getDate()
}()
},
listeners:{
onBeforeShow: function() {},
onChange: function() {}
}
})
</script>
這個控件透過初始化時傳入的 options 進行配置,例如 showWeek 可選擇是否顯示週次,maxDate 則能限制可選擇的最大日期(此例設為一年後)。此外還提供了 onBeforeShow、onChange 等事件回呼,方便在顯示前或選擇後執行自訂邏輯。