现在的位置: 首页 > 综合 > 正文

Jquery ui Datepicker 使用心得

2013年02月28日 ⁄ 综合 ⁄ 共 2981字 ⁄ 字号 评论关闭

 

 

1 <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
2   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
3   <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
4   <script src="js/Datepicker/jquery.ui.datepicker-zh-CN.js" type="text/javascript"></script>
5   <link href="css/datepicker.css" rel="stylesheet" type="text/css" />
6   <script>
7 $(function() {
8 $("#datepicker").datepicker({
9 showMonthAfterYear: true,
10 changeMonth: true,
11 changeYear: true,
12 buttonImageOnly: true,
13 minDate: '-1y', maxDate: '+2y',
14 dateFormat: 'yy-mm-dd',
15 onSelect: function(dateText, inst) {
16 $.ajax({ url: "hx/Datepicker.ashx?rel=1", success: function() {
17 alert('ee');
18 }
19 });
20 }
21 });
22 });
23 </script>

 

 

 

 

 

1-5

  按照先后顺序,导入 Datepicker 需要的准备文件。

 

PASS:

4

  日历控件的中文显示 ,jquery官网提供

 

中文显示

1 jQuery(function($){
2 $.datepicker.regional['zh-CN'] = {
3 closeText: '关闭',
4 prevText: '&#x3c;上月',
5 nextText: '下月&#x3e;',
6 currentText: '今天',
7 monthNames: ['一月','二月','三月','四月','五月','六月',
8 '七月','八月','九月','十月','十一月','十二月'],
9 monthNamesShort: ['','','','','','',
10 '','','','','十一','十二'],
11 dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
12 dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],
13 dayNamesMin: ['','','','','','',''],
14 weekHeader: '',
15 dateFormat: 'yy-mm-dd',
16 firstDay: 1,
17 isRTL: false,
18 showMonthAfterYear: true,
19 yearSuffix: ''};
20 $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
21 });
22  

 

 

5

  在使用国际化同时启用changeYear和changeMonth后,两个select显示为两行

  在官方提供的jquery-ui-1.7.2.custom.css中,应该作如下几处修改: 
.ui-datepicker .ui-datepicker-title select { float:left; font-size:1em; margin:1px 0; } 
.ui-datepicker select.ui-datepicker-month-year {width: 100%;} 
.ui-datepicker select.ui-datepicker-month, 
.ui-datepicker select.ui-datepicker-year { width: 49%;} 
.ui-datepicker .ui-datepicker-title select.ui-datepicker-year { float: right; } 
应该改为: 

.ui-datepicker .ui-datepicker-title select {font-size:1em; margin:1px 0; }   
.ui-datepicker select.ui-datepicker-month-year {width: 100%;}   
.ui-datepicker select.ui-datepicker-month{margin-left:4px;}
.ui-datepicker select.ui-datepicker-month{ width: 40%;vertical-align:top;}
.ui-datepicker select.ui-datepicker-year { width: 43%;vertical-align:top;}

.ui-datepicker .ui-datepicker-title select.ui-datepicker-year { float: left; }   
ui-datepicker-title{vertical-align:middle;}


就OK了 (该样式根据当前项目布局自己修改,我对css不是很了解,看起来差不多就行了)


6-23

  我的项目对日历控件的代码调整

 

9-14

  是对控件属性的设置

showMonthAfterYear: true// 月在年之后显示      

changeMonth: true,   // 允许选择月份     

changeYear: true,   // 允许选择年份     

dateFormat:'yy-mm-dd',  // 设置日期格式  

 

15

  设置选择事件,调用了ajax get 的传送事件

 $.ajax({ url: "hx/Datepicker.ashx?rel=1", success: function(response) {
                      alert(response);
                  }});

 

Datepicker.ashx一般处理程序传递一个参数

success callback方法 当传递成功返回的方法

 

  返回的数据

success: function(response) {
                      alert(response);
                  }

 

pass:在对datepicker初始化的时候设置年份的范围

如果采用

minDate: '-1y', maxDate: '+2y',

设置会出现一个问题,最后的一个年份只会显示出一个月

解决办法: yearRange:'2010:2012',
用这个设置 最后的年份则会显示全部的12个月.

     

抱歉!评论已关闭.