雅荷心语博客
雅荷心语
心之所向便是光
  • 首页
  • 前端之旅
  • 后端之路
  • 软件工具
  • 心灵鸡汤
  • 心路历程
  • 视频资料
  • 关于我们
    • 关于我
    • 关于我
  • 微信平台
    • 业务合作
  • 首页
  • 前端之旅
  • 后端之路
  • 软件工具
  • 心灵鸡汤
  • 心路历程
  • 视频资料
  • 关于我们
    • 关于我
    • 关于我
  • 微信平台
    • 业务合作
  • 关注本站
    • 微信
    • 微博
    • 腾讯微博
    • Twitter
    • Facebook
    • RSS订阅
Hi, 请登录     我要注册     找回密码

JQuery datepicker 的一些简单用法

2015-12-08 分类:前端之旅 阅读(2395) 评论(0)

jQuery UI很强大,其中的日期选择插件Datepicker是一个配置灵活的插件,我们可以自定义其展示方式,包括日期格式、语言、限制选择日期范围、添加相关按钮以及其它导航等。
官方地址:http://docs.jquery.com/UI/Datepicker,官方示例: http://jqueryui.com/demos/datepicker/。
一个不错的地址,用来DIY jQuery UI界面效果的站点http://jqueryui.com/themeroller/
从昨天晚上开始看了看这个插件,一直打不到[呸,应该是达不到]想要的效果,今天早上从新理了理思路,打到了想要的效果;
当第一个选择框选择日期后;第二个框的日期不能小于第一个框,而且不能大于当前月,也就是说如果用户选择了12月1日,那第二个框,用户只能选择到12月的最后一天
勉强实现效果,代码如下:

Js部分
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$(function(){
var maxDate = new Date();
$( "#from" ).datepicker({
changeMonth: true, //当年份或月份改变时触发此事件
numberOfMonths: 1, //设置选择时间的月份范围,比如一个月
maxDate:maxDate,
onClose: function( selectedDate ) {
$("#to").datepicker('option', 'stepMonths', 0); //一次显示几个月
$("#to").datepicker('option', 'changeMonth', false); //不允许下拉框选择月份
$("#to").datepicker( "option", "minDate",selectedDate ); //minDate 表示设置一个最大的可选日期
$("#from").datepicker('option', 'altField', '#to'); //设置到另一个域
$("#to").datepicker('option', 'hideIfNoPrevNext', true); //隐藏无法点击的那个月
$(this).attr('isChose',1);//选择锁
layer.msg('您选择了开始时间'+ selectedDate)
}
});
$("#to").datepicker({
maxDate:maxDate,
changeMonth: true,
numberOfMonths: 1,
beforeShow:function(selectedDate){ //选择之前进行判断
if(!$( "#from" ).attr('isChose')){layer.msg('请先选择开始日期');return false;}
},
onClose: function( selectedDate ) {
$( "#from" ).datepicker( "option", "month", selectedDate );
console.log(new Date());
layer.msg('您选择了结束时间'+ selectedDate)
}
});
});

html部分
XHTML
1
2
3
4
5
6
<tr class="col-xs-12">
    <td height="40" align="right">有效期起始:</td>
    <td height="40" align="left"><input type="text" name="te" id="from" class="test-style width150 form-control" /></td>
    <td height="40" align="right">有效期截止:</td>
    <td height="40" align="left"><input type="text" name="tex" id="to" class="test-style width150 form-control" /></td>
  </tr>

文档 : http://www.cnblogs.com/lf6112/archive/2011/05/19/2051126.html

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
altField : String : ''
  将选择的日期同步到另一个域中,配合altFormat可以显示不同格式的日期字符串。
  初始:$('.selector').datepicker({ altField: '#actualDate' });
  获取:var altField = $('.selector').datepicker('option', 'altField');
  设置:$('.selector').datepicker('option', 'altField', '#actualDate');  
 
altFormat : String : ''
  当设置了altField的情况下,显示在另一个域中的日期格式。
  初始:$('.selector').datepicker({ altFormat: 'yy-mm-dd' });
  获取:var altFormat = $('.selector').datepicker('option', 'altFormat');
  设置:$('.selector').datepicker('option', 'altFormat', 'yy-mm-dd');  
 
appendText : String : ''
  在日期插件的所属域后面添加指定的字符串。
  初始:$('.selector').datepicker({ appendText: '(yyyy-mm-dd)' });
  获取:var appendText = $('.selector').datepicker('option', 'appendText');
  设置:$('.selector').datepicker('option', 'appendText', '(yyyy-mm-dd)');  
 
buttonImage : String : ''
  设置弹出按钮的图片,如果非空,则按钮的文本将成为alt属性,不直接显示。
  初始:$('.selector').datepicker({ buttonImage: '/images/datepicker.gif' });
  获取:var buttonImage = $('.selector').datepicker('option', 'buttonImage');
  设置:$('.selector').datepicker('option', 'buttonImage', '/images/datepicker.gif');  
 
buttonImageOnly : Boolean : false
  Set to true to place an image after the field to use as the trigger without it appearing on a button.
  初始:$('.selector').datepicker({ buttonImageOnly: true });
  获取:var buttonImageOnly = $('.selector').datepicker('option', 'buttonImageOnly');
  设置:$('.selector').datepicker('option', 'buttonImageOnly', true);  
 
buttonText : String : '...'
  设置触发按钮的文本内容。
  初始:$('.selector').datepicker({ buttonText: 'Choose' });
  获取:var buttonText = $('.selector').datepicker('option', 'buttonText');
  设置:$('.selector').datepicker('option', 'buttonText', 'Choose');  
 
changeMonth : Boolean : false
  设置允许通过下拉框列表选取月份。
  初始:$('.selector').datepicker({ changeMonth: true });
  获取:var changeMonth = $('.selector').datepicker('option', 'changeMonth');
  设置:$('.selector').datepicker('option', 'changeMonth', true);  
 
changeYear : Boolean : false
  设置允许通过下拉框列表选取年份。
  初始:$('.selector').datepicker({ changeYear: true });
  获取:var changeYear = $('.selector').datepicker('option', 'changeYear');
  设置:$('.selector').datepicker('option', 'changeYear', true);  
 
closeTextType: StringDefault: 'Done'
  设置关闭按钮的文本内容,此按钮需要通过showButtonPanel参数的设置才显示。
  初始:$('.selector').datepicker({ closeText: 'X' });
  获取:var closeText = $('.selector').datepicker('option', 'closeText');
  设置:$('.selector').datepicker('option', 'closeText', 'X');  
 
constrainInput : Boolean : true
  如果设置为true,则约束当前输入的日期格式。
  初始:$('.selector').datepicker({ constrainInput: false });
  获取:var constrainInput = $('.selector').datepicker('option', 'constrainInput');
  设置:$('.selector').datepicker('option', 'constrainInput', false);  
 
currentText : String : 'Today'
  设置当天按钮的文本内容,此按钮需要通过showButtonPanel参数的设置才显示。
  初始:$('.selector').datepicker({ currentText: 'Now' });
  获取:var currentText = $('.selector').datepicker('option', 'currentText');
  设置:$('.selector').datepicker('option', 'currentText', 'Now');  
 
dateFormat : String : 'mm/dd/yy'
  设置日期字符串的显示格式。
  初始:$('.selector').datepicker({ dateFormat: 'yy-mm-dd' });
  获取:var dateFormat = $('.selector').datepicker('option', 'dateFormat');
  设置:$('.selector').datepicker('option', 'dateFormat', 'yy-mm-dd');  
 
dayNames : Array : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  设置一星期中每天的名称,从星期天开始。此内容用于dateFormat时显示,以及日历中当鼠标移至行头时显示。
  初始:$('.selector').datepicker({ dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'] });
  获取:var dayNames = $('.selector').datepicker('option', 'dayNames');
  设置:$('.selector').datepicker('option', 'dayNames', ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi']);  
 
dayNamesMin : Array : ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
  设置一星期中每天的缩语,从星期天开始,此内容用于dateFormat时显示,以前日历中的行头显示。
  初始:$('.selector').datepicker({ dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'] });
  获取:var dayNamesMin = $('.selector').datepicker('option', 'dayNamesMin');
  设置:$('.selector').datepicker('option', 'dayNamesMin', ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa']);  
 
dayNamesShort : Array : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
  设置一星期中每天的缩语,从星期天开始,此内容用于dateFormat时显示,以前日历中的行头显示。
  初始:$('.selector').datepicker({ dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'] });
  获取:var dayNamesShort = $('.selector').datepicker('option', 'dayNamesShort');
  设置:$('.selector').datepicker('option', 'dayNamesShort', ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam']);  
 
defaultDate : Date, Number, String : null
  设置默认加载完后第一次显示时选中的日期。可以是Date对象,或者是数字(从今天算起,例如+7),或者有效的字符串('y'代表年, 'm'代表月, 'w'代表周, 'd'代表日, 例如:'+1m +7d')。
  初始:$('.selector').datepicker({ defaultDate: +7 });
  获取:var defaultDate = $('.selector').datepicker('option', 'defaultDate');
  设置:$('.selector').datepicker('option', 'defaultDate', +7);  
 
duration : String, Number : 'normal'
  设置日期控件展开动画的显示时间,可选是"slow", "normal", "fast",''代表立刻,数字代表毫秒数。
  初始:$('.selector').datepicker({ duration: 'slow' });
  获取:var duration = $('.selector').datepicker('option', 'duration');
  设置:$('.selector').datepicker('option', 'duration', 'slow');  
 
firstDay : Number : 0
  设置一周中的第一天。星期天为0,星期一为1,以此类推。
  初始:$('.selector').datepicker({ firstDay: 1 });
  获取:var firstDay = $('.selector').datepicker('option', 'firstDay');
  设置:$('.selector').datepicker('option', 'firstDay', 1);  
 
gotoCurrent : Boolean : false
  如果设置为true,则点击当天按钮时,将移至当前已选中的日期,而不是今天。
  初始:$('.selector').datepicker({ gotoCurrent: true });
  获取:var gotoCurrent = $('.selector').datepicker('option', 'gotoCurrent');
  设置:$('.selector').datepicker('option', 'gotoCurrent', true);  
 
hideIfNoPrevNext : Boolean : false
  设置当没有上一个/下一个可选择的情况下,隐藏掉相应的按钮。(默认为不可用)
  初始:$('.selector').datepicker({ hideIfNoPrevNext: true });
  获取:var hideIfNoPrevNext = $('.selector').datepicker('option', 'hideIfNoPrevNext');
  设置:$('.selector').datepicker('option', 'hideIfNoPrevNext', true);  
 
isRTL : Boolean : false
  如果设置为true,则所有文字是从右自左。
  初始:$('.selector').datepicker({ isRTL: true });
  获取:var isRTL = $('.selector').datepicker('option', 'isRTL');
  设置:$('.selector').datepicker('option', 'isRTL', true);  
 
maxDate : Date, Number, String : null
  设置一个最大的可选日期。可以是Date对象,或者是数字(从今天算起,例如+7),或者有效的字符串('y'代表年, 'm'代表月, 'w'代表周, 'd'代表日, 例如:'+1m +7d')。
  初始:$('.selector').datepicker({ maxDate: '+1m +1w' });
  获取:var maxDate = $('.selector').datepicker('option', 'maxDate');
  设置:$('.selector').datepicker('option', 'maxDate', '+1m +1w');
       $('.selector').datepicker('option', 'maxDate', '12/25/2012');  
 
minDate : Date, Number, String : null
  设置一个最小的可选日期。可以是Date对象,或者是数字(从今天算起,例如+7),或者有效的字符串('y'代表年, 'm'代表月, 'w'代表周, 'd'代表日, 例如:'+1m +7d')。
  初始:$('.selector').datepicker({ minDate: new Date(2007, 1 - 1, 1) });
  获取:var minDate = $('.selector').datepicker('option', 'minDate');
  设置:$('.selector').datepicker('option', 'minDate', new Date(2007, 1 - 1, 1));
       $('.selector').datepicker('option', 'minDate', '12/25/2012');  
 
monthNames : Array : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  设置所有月份的名称。
  初始:$('.selector').datepicker({monthNames:['Januar','Februar','Marts','April','Maj','Juni','Juli','August','September','Oktober','November','December']});
  获取:var monthNames = $('.selector').datepicker('option', 'monthNames');
  设置:$('.selector').datepicker('option', 'monthNames', ['Januar','Februar','Marts','April','Maj','Juni','Juli','August','September','Oktober','November','December']);  
 
monthNamesShort : Array : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  设置所有月份的缩写。
  初始:$('.selector').datepicker({monthNamesShort:['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec']});
  获取:var monthNamesShort = $('.selector').datepicker('option', 'monthNamesShort');
  设置:$('.selector').datepicker('option', 'monthNamesShort', ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec']);  
 
navigationAsDateFormat : Boolean : false
  如果设置为true,则formatDate函数将应用到 prevText,nextText和currentText的值中显示,例如显示为月份名称。
  初始:$('.selector').datepicker({ navigationAsDateFormat: true });
  获取:var navigationAsDateFormat = $('.selector').datepicker('option', 'navigationAsDateFormat');
  设置:$('.selector').datepicker('option', 'navigationAsDateFormat', true);  
 
nextText : String : 'Next'
  设置“下个月”链接的显示文字。
  初始:$('.selector').datepicker({ nextText: 'Later' });
  获取:var nextText = $('.selector').datepicker('option', 'nextText');
  设置:$('.selector').datepicker('option', 'nextText', 'Later');  
 
numberOfMonths : Number, Array : 1
  设置一次要显示多少个月份。如果为整数则是显示月份的数量,如果是数组,则是显示的行与列的数量。
  初始:$('.selector').datepicker({ numberOfMonths: [2, 3] });
  获取:var numberOfMonths = $('.selector').datepicker('option', 'numberOfMonths');
  设置:$('.selector').datepicker('option', 'numberOfMonths', [2, 3]);  
 
prevText : String : 'Prev'
  设置“上个月”链接的显示文字。
  初始:$('.selector').datepicker({ prevText: 'Earlier' });
  获取:var prevText = $('.selector').datepicker('option', 'prevText');
  设置:$('.selector').datepicker('option', 'prevText', 'Earlier');  
 
shortYearCutoff : String, Number : '+10'
  设置截止年份的值。如果是(0-99)的数字则以当前年份开始算起,如果为字符串,则相应的转为数字后再与当前年份相加。当超过截止年份时,则被认为是上个世纪。
  初始:$('.selector').datepicker({ shortYearCutoff: 50 });
  获取:var shortYearCutoff = $('.selector').datepicker('option', 'shortYearCutoff');
  设置:$('.selector').datepicker('option', 'shortYearCutoff', 50);  
 
showAnim : String : 'show'
  设置显示、隐藏日期插件的动画的名称。
  初始:$('.selector').datepicker({ showAnim: 'fold' });
  获取:var showAnim = $('.selector').datepicker('option', 'showAnim');
  设置:$('.selector').datepicker('option', 'showAnim', 'fold');  
 
showButtonPanel : Boolean : false
  设置是否在面板上显示相关的按钮。
  初始:$('.selector').datepicker({ showButtonPanel: true });
  获取:var showButtonPanel = $('.selector').datepicker('option', 'showButtonPanel');
  设置:$('.selector').datepicker('option', 'showButtonPanel', true);  
 
showCurrentAtPos : Number : 0
  设置当多月份显示的情况下,当前月份显示的位置。自顶部/左边开始第x位。
  初始:$('.selector').datepicker({ showCurrentAtPos: 3 });
  获取:var showCurrentAtPos = $('.selector').datepicker('option', 'showCurrentAtPos');
  设置:$('.selector').datepicker('option', 'showCurrentAtPos', 3);  
 
showMonthAfterYear : Boolean : false
  是否在面板的头部年份后面显示月份。
  初始:$('.selector').datepicker({ showMonthAfterYear: true });
  获取:var showMonthAfterYear = $('.selector').datepicker('option', 'showMonthAfterYear');
  设置:$('.selector').datepicker('option', 'showMonthAfterYear', true);  
 
showOn : String : 'focus'
  设置什么事件触发显示日期插件的面板,可选值:focus, button, both
  初始:$('.selector').datepicker({ showOn: 'both' });
  获取:var showOn = $('.selector').datepicker('option', 'showOn');
  设置:$('.selector').datepicker('option', 'showOn', 'both');  
 
showOptions : Options : {}
  如果使用showAnim来显示动画效果的话,可以通过此参数来增加一些附加的参数设置。
  初始:$('.selector').datepicker({ showOptions: {direction: 'up' });
  获取:var showOptions = $('.selector').datepicker('option', 'showOptions');
  设置:$('.selector').datepicker('option', 'showOptions', {direction: 'up');  
 
showOtherMonths : Boolean : false
  是否在当前面板显示上、下两个月的一些日期数(不可选)。
  初始:$('.selector').datepicker({ showOtherMonths: true });
  获取:var showOtherMonths = $('.selector').datepicker('option', 'showOtherMonths');
  设置:$('.selector').datepicker('option', 'showOtherMonths', true);  
 
stepMonths : Number : 1
  当点击上/下一月时,一次翻几个月。
  初始:$('.selector').datepicker({ stepMonths: 3 });
  获取:var stepMonths = $('.selector').datepicker('option', 'stepMonths');
  设置:$('.selector').datepicker('option', 'stepMonths', 3);  
 
yearRange : String : '-10:+10'
  控制年份的下拉列表中显示的年份数量,可以是相对当前年(-nn:+nn),也可以是绝对值 (-nnnn:+nnnn)
  初始:$('.selector').datepicker({ yearRange: '2000:2010' });
  获取:var yearRange = $('.selector').datepicker('option', 'yearRange');
  设置:$('.selector').datepicker('option', 'yearRange', '2000:2010');
 
beforeShow : function(input)
  在日期控件显示面板之前,触发此事件,并返回当前触发事件的控件的实例对象。
  初始:$('.selector').datepicker({ beforeShow: function(input) { ... } });  
 
beforeShowDay : function(date)
  在日期控件显示面板之前,每个面板上的日期绑定时都触发此事件,参数为触发事件的日期。调用函数后,必须返回一个数组:[0]此日期是否可选(true/false),[1]此日期的CSS样式名称(""表示默认),[2]当鼠标移至上面出现一段提示的内容。
  初始:$('.selector').datepicker({ beforeShowDay: function(date) { ... } });  
 
onChangeMonthYear : function(year, month, inst)
  当年份或月份改变时触发此事件,参数为改变后的年份月份和当前日期插件的实例。
  初始:$('.selector').datepicker({ onChangeMonthYear: function(year, month, inst) { ... } });  
 
onClose : function(dateText, inst)
  当日期面板关闭后触发此事件(无论是否有选择日期),参数为选择的日期和当前日期插件的实例。
  初始:$('.selector').datepicker({ onClose: function(dateText, inst) { ... } });  
 
onSelect : function(dateText, inst)
  当在日期面板选中一个日期后触发此事件,参数为选择的日期和当前日期插件的实例。
  $('.selector').datepicker({ onSelect: function(dateText, inst) { ... } });

QQ截图20151130153809

分享到:更多 ()

相关推荐

  • AI 编辑器 cursor 如何禁止自动更新
  • AI 编辑器 cursor 工具栏改成和 vscode 一样的左侧 竖向展示
  • nodejs 脚本打包为可执行文件
  • 初学 python 笔记
  • nodejs 同时运行多个脚本
  • 让你的照片动起来
  • vue工程项目动态加载umd.js实践
  • 使用 shell 检测目标服务器是否异常
关于我

小天明 北京·朝阳 前端搬砖工程师

碎碎念):(来自公众号)

热门文章

  • 踩坑记录——iphone上safari开启隐身模式时localStorage变为只读-雅荷心语博客踩坑记录——iphone上safari开启隐身模式时localStorage变为只读2017-02-21评论(4)
  • 程序员是怎样一群人-雅荷心语博客程序员是怎样一群人2015-12-08评论(3)
  • 百度你个大毒瘤 - 吐糟博客这几天打不开事情-雅荷心语博客百度你个大毒瘤 – 吐糟博客这几天打不开事情2015-12-28评论(2)
  • PHP 非对称加密 openssl 加密及解密方法-雅荷心语博客PHP 非对称加密 openssl 加密及解密方法2016-05-17评论(2)
  • PHPStorm10 下载安装破解汉化-雅荷心语博客PHPStorm10 下载安装破解汉化2015-12-15评论(2)
2025年7月
一 二 三 四 五 六 日
« 六    
 123456
78910111213
14151617181920
21222324252627
28293031  

最新评论

  • 前端小武 8年前 (2017-04-06)说:
    我看到了layer
  • 丁艳平 8年前 (2017-03-03)说:
  • Dawn 9年前 (2016-09-16)说:
    call_user_func_array最后的例子是错哦,你用bc方法去调用类里 另外一个方法就知道问题所在了。情况1.调用非静态方法 第一个参数应该传[类的实例,调用方法] (既然有类实例了直接-&
  • Dawn 9年前 (2016-06-21)说:
    tp框架设置了全局捕获异常的,这也没什么。坑的是 他捕获了异常。然后全部返回404。。。不知道的 还以为自己网站被删除了
  • Dawn 9年前 (2016-05-17)说:
    构造函数里的判断 用异常机制可能更好一些

其他类型

  • 芊云全景
  • 配音兔AI配音神器

博客类型

  • 芊云全景
  • 配音兔AI配音神器

左邻右舍

  • 易水寒
  • 楼教主
  • 芊云全景
  • 贤心
  • 配音兔AI配音神器

雅荷心语博客 -心之所向便是光

联系我们关于我们

© 2025 雅荷心语博客   网站地图