jquery日期控制項使用,起止時間

來源:互聯網
上載者:User

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檔案:

<!-- 引入 jQuery --><script type='text/javascript' src='<%=path%>/firecity/js/jquery-1.7.1.min.js'></script><script src="<%=path%>/firecity/js/devidepage.js" type="text/javascript"></script><!--添加datepicker支援--> <script src="<%=path %>/firecity/js/jquery.ui.datepicker.js" type="text/javascript"></script><script src="<%=path %>/firecity/js/jquery.ui.core.js" type="text/javascript"></script><!-- 添加中文支援--><script src="<%=path %>/firecity/js/jquery.ui.datepicker-zh-CN.js" type="text/javascript"></script>

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

<link rel="stylesheet" href="<%=path %>/firecity/css/jqueryUI/jquery-ui.css" type="text/css"><link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>

4.在HTML中插入文本域,最好設定成唯讀,不接受使用者的手動輸入,防止格式混亂,以id標記好。
日期 :   <input  type="text" size="10" maxlength="10" class="fromdate"  id="fromdate" readonly="readonly"/>  至 <input type="text" size="10" maxlength="10" class="enddate" id="enddate"  readonly="readonly"/>

5、編寫js代碼,實現最終效果
$(document).ready(function() {           $('#fromdate').datepicker();        $('#enddate').datepicker();    }); 
這裡只是做了一個最基本的日期控制項,我們還需要以中文顯示,限制日期選擇範圍等需求,稍微修改js代碼:

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

    jQuery(function($){          $.datepicker.regional['zh-CN'] = {          clearText: '清除', clearStatus: '清除已選日期',            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']);      });  

完整的頁面代碼如下:
    <!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 --><script type='text/javascript' src='<%=path%>/firecity/js/jquery-1.7.1.min.js'></script><script src="<%=path%>/firecity/js/devidepage.js" type="text/javascript"></script><!--添加datepicker支援--> <script src="<%=path %>/firecity/js/jquery.ui.datepicker.js" type="text/javascript"></script><script src="<%=path %>/firecity/js/jquery.ui.core.js" type="text/javascript"></script><!-- 添加中文支援--><script src="<%=path %>/firecity/js/jquery.ui.datepicker-zh-CN.js" type="text/javascript"></script>                       <!--引入樣式css-->         <link rel="stylesheet" href="<%=path %>/firecity/css/jqueryUI/jquery-ui.css" type="text/css">   <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>                    <script type='text/javascript'><!--          //等待dom元素載入完畢.              $(document).ready(function() {     $(".fromdata").datepicker({//添加日期選擇功能     numberOfMonths:1,//顯示幾個月  showButtonPanel:true,//是否顯示按鈕面板  showClearButton: true ,changeMonth:false,defaultDate:+1, //   showWeek: true, howOn:"button", //borth 既可以觸發按鈕 又可以觸發文字框 彈出 日曆  如果是button 只能觸發button事件  buttonImageOnly: true,       //設定這按鈕只顯示圖片效果 不要有button的樣式  showAnim:"toggle", //彈出日曆的效果buttonText: 'Choose',hideIfNoPrevNext: true,dateFormat: 'yy-mm-dd',//日期格式  clearText:"清除",//清除日期的按鈕名稱  closeText:"關閉",//關閉選擇框的按鈕名稱  yearSuffix: '年', //年的尾碼  showMonthAfterYear:true,//是否把月放在年的後面  defaultDate:'2013-03-10',//預設日期  minDate:'2014-03-05',//最小日期  maxDate:'2024-03-20',//最大日期  onSelect: function( selectedDate ) {   $( ".enddata" ).datepicker( "option", "minDate", new Date(selectedDate.replace(/-/g,',')) );//結束時間可選最小值為選中值} });    $(".enddata").datepicker({//添加日期選擇功能  numberOfMonths:1,//顯示幾個月  showButtonPanel:true,//是否顯示按鈕面板  showClearButton: true ,changeMonth:false,defaultDate:+1, //   showWeek: true, howOn:"button", //borth 既可以觸發按鈕 又可以觸發文字框 彈出 日曆  如果是button 只能觸發button事件  buttonImageOnly: true,       //設定這按鈕只顯示圖片效果 不要有button的樣式  showAnim:"toggle", //彈出日曆的效果buttonText: 'Choose',hideIfNoPrevNext: true,dateFormat: 'yy-mm-dd',//日期格式  clearText:"清除",//清除日期的按鈕名稱  closeText:"關閉",//關閉選擇框的按鈕名稱  yearSuffix: '年', //年的尾碼  showMonthAfterYear:true,//是否把月放在年的後面  defaultDate:'2013-03-10',//預設日期  minDate:'2014-03-05',//最小日期  maxDate:'2024-03-20',//最大日期  onSelect: function( selectedDate ) {$( ".fromdata" ).datepicker( "option", "maxDate", new Date(selectedDate.replace(/-/g,',')) );//起始時間可選最大值為選中值} });});               // --></script>       </HEAD>       <BODY>        日期 :   <input  type="text" size="10" maxlength="10" class="fromdate"  id="fromdate" readonly="readonly"/>  至 <input type="text" size="10" maxlength="10" class="enddate" id="enddate"  readonly="readonly"/>     </BODY>      </HTML> 

注意:如果不加上

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> 

會導致日期控制項中的選擇上下月的表徵圖不能正常顯示,是因為本地沒有可引用的圖片資源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.