天天看點

JQuery日期插件datepicker的使用

jquery是一款非常優秀的腳本架構,其豐富的控件使用起來也非常簡單,配置非常靈活。下面做一個使用日期插件datapicker的例子。 

1、下載下傳jquery核心檔案就不用說了吧,datepicker是輕量級插件,隻需jquery的min版本就行了,然後到官網http://jqueryui.com/download下載下傳jquery-ui壓縮包(可以選擇喜歡的theme),裡面就包含對datepicker的支援,當然您也可以網站http://marcgrabanski.com/pages/code/jquery-ui-datepicker下載下傳datepicker,包括ui.core.js和ui.datepicker.js。

2、在html中引用下載下傳下來的js檔案: 

[xhtml] view

plaincopy

       <!-- 引入 jquery -->  

<mce:script src="js/jquery.1.4.2.js" mce_src="js/jquery-1.5.1.min.js" type="text/javascript"></mce:script>  

<!--添加datepicker支援-->  

<mce:script src="js/jquery.ui.core.js" mce_src="js/jquery.ui.core.js" type="text/javascript"></mce:script>  

<mce:script src="js/jquery.ui.datepicker.js" mce_src="js/jquery.ui.datepicker.js" type="text/javascript"></mce:script>  

3.在html中引入預設樣式表檔案,這個檔案在ui壓縮包中。如果在官網下載下傳,首頁就有這個css檔案下載下傳,也可選擇其他皮膚的css。

<!--引入樣式css-->  

k type="text/css" rel="stylesheet" href="css/jquery-ui-1.8.13.custom.css" mce_href="css/jquery-ui-1.7.3.custom.css" />  

4.在html中插入文本域,最好設定成隻讀,不接受使用者的手動輸入,防止格式混亂,以id标記好。

<input type="text" id="selectdate" readonly="readonly"/>  

5.編寫js代碼,實作最終效果。

$(document).ready(function() {     

      $('#selectdate').datepicker();     

  });  

效果如下圖:

JQuery日期插件datepicker的使用

這裡隻是做了一個最基本的日期控件,我們還需要以中文顯示,限制日期選擇範圍等需求,稍微修改js代碼:

[javascript] view

<mce:script type="text/javascript"><!--  

    //等待dom元素加載完畢.  

        $(function(){  

            $("#selectdate").datepicker({//添加日期選擇功能  

            numberofmonths:1,//顯示幾個月  

            showbuttonpanel:true,//是否顯示按鈕面闆  

            dateformat: 'yy-mm-dd',//日期格式  

            cleartext:"清除",//清除日期的按鈕名稱  

            closetext:"關閉",//關閉選擇框的按鈕名稱  

            yearsuffix: '年', //年的字尾  

            showmonthafteryear:true,//是否把月放在年的後面  

            defaultdate:'2011-03-10',//預設日期  

            mindate:'2011-03-05',//最小日期  

            maxdate:'2011-03-20',//最大日期  

            monthnames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],  

            daynames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],  

            daynamesshort: ['周日','周一','周二','周三','周四','周五','周六'],  

            daynamesmin: ['日','一','二','三','四','五','六'],  

            onselect: function(selecteddate) {//選擇日期後執行的操作  

                alert(selecteddate);  

            }  

            });  

        });  

// --></mce:script>  

效果如下:

JQuery日期插件datepicker的使用

這裡基本上就滿足我們使用的需要了。datepicker控件預設是英文的,可以在構造datepicker時通過monthnames、daynames屬性來指定月、日的中文顯示值,但是每次使用是都配置這些屬性不免太麻煩了,可以增加一個js檔案将中文配置都放在裡面,每次使用直接引用即可,這裡放在jquery.ui.datepicker-zh-cn.js中,内容如下:

jquery(function($){  

    $.datepicker.regional['zh-cn'] = {  

        closetext: '關閉',  

        prevtext: '<上月',  

        nexttext: '下月>',  

        currenttext: '今天',  

        monthnames: ['一月','二月','三月','四月','五月','六月',  

        '七月','八月','九月','十月','十一月','十二月'],  

        monthnamesshort: ['一','二','三','四','五','六',  

        '七','八','九','十','十一','十二'],  

        daynames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],  

        daynamesshort: ['周日','周一','周二','周三','周四','周五','周六'],  

        daynamesmin: ['日','一','二','三','四','五','六'],  

        weekheader: '周',  

        dateformat: 'yy-mm-dd',  

        firstday: 1,  

        isrtl: false,  

        showmonthafteryear: true,  

        yearsuffix: '年'};  

    $.datepicker.setdefaults($.datepicker.regional['zh-cn']);  

});  

6.在頁面中引入中文插件

<!-- 添加中文支援-->  

    <mce:script src="js/jquery.ui.datepicker-zh-cn.js" mce_src="js/jquery.ui.datepicker-zh-cn.js" type="text/javascript"></mce:script>  

完整的頁面代碼如下:

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">  

<html>  

 <head>  

 <meta http-equiv="content-type" content="text/html; charset=utf-8" />  

  <title>日期控件datepicker</title>  

    <!-- 引入 jquery -->  

    <mce:script src="js/jquery.1.4.2.js" mce_src="js/jquery.1.4.2.js" type="text/javascript"></mce:script>  

    <!--添加datepicker支援-->  

    <mce:script src="js/jquery.ui.core.js" mce_src="js/jquery.ui.core.js" type="text/javascript"></mce:script>  

    <mce:script src="js/jquery.ui.datepicker.js" mce_src="js/jquery.ui.datepicker.js" type="text/javascript"></mce:script>  

    <!-- 或者引入jquery ui包,其中也包含對datepicker的支援  

    <mce:script src="js/jquery-ui-1.7.3.custom.min.js" mce_src="js/jquery-ui-1.7.3.custom.min.js" type="text/javascript"></mce:script>  

    -->  

    <!--引入樣式css-->  

    <link type="text/css" rel="stylesheet" href="css/jquery-ui-1.7.3.custom.css" mce_href="css/jquery-ui-1.7.3.custom.css" />  

    <!-- 添加中文支援-->  

    <mce:script type="text/javascript"><!--  

            //monthnames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],  

            //daynames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],  

            //daynamesshort: ['周日','周一','周二','周三','周四','周五','周六'],  

            //daynamesmin: ['日','一','二','三','四','五','六'],  

 </head>  

 <body>  

  <input type="text" id="selectdate" readonly="readonly"/>  

 </body>  

</html>  

注意:由于jquery datepicker首頁http://marcgrabanski.com/articles/jquery-ui-datepicker上ui.core.js和ui.datepicker.js不是最新版本的,如果下載下傳新版本jquery-ui-1.8.13中的css檔案會造成日期控件不能顯示的問題,是以這裡使用了1.7.3的ui。簡單一點就是用jquery-ui的壓縮js。

繼續閱讀