
正文
jquery ui widgets-datepicker
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
jquery ui的用法就不在此讲述,直接进入jquery ui的窗体小部件(widgets)——datepicker。
相信很多像我这样子的菜鸟少年,如果同一个页面上有两个input文本输入框是用来输入日期的话,那么我都是直接给这两个input文本输入框一个id,然后使用这两个id来进行datepicker的初始化设置。
一般情况下,我都是按照下面的代码写的,但是慢慢地发现,老是这样子写多麻烦呀!难道就不能只使用一个div来加载一个query ui的datepicker吗?然后两个或者多个input共用这一个日历来获取值。于是,就产生了与这下面代码的另一段代码。我知道我是菜鸟,所以也是写给菜鸟看的,欢迎高手指点,不甚感激!
代码一:
1 <!--加载jquery,jquery ui的css文件以及js文件省略-->
2 <script type="text/javascript">
3 $('#check_in').datepicker({
4 minDate : new Date(),
5 dateFormat : 'yy-mm-dd',
6 onSelect : function(dateText,inst){
7 $('#check_in').val(dateText);
8 }
9 });
10
11 $('#check_out').datepicker({
12 minDate : new Date(),
13 dateFormat : 'yy-mm-dd',
14 onSelect : function(dateText,inst){
15 $('#check_out').val(dateText);
16 }
17 });
18
19 $('#check_in').focusin(function(){
20 ......
21 //具体操作不写
22 });
23
24 $('#check_out').focusin(function(){
25 ......
26 //具体操作不写
27 })
28
29 </script>
30
31 <input type="text" name="check_in" id="check_in" />
32 <input type="text" name="check_out" id="check_out" />
代码二:
一般的使用情况下,我自认为只要有一个datepicker就足够了。除非你的每个输入框要求的日期时间格式要求差异化了,不过相信这种需求很少的。既然这样子的话,那么我们就完全可以使用一个datepicker,配合datepicker的Function option实现动态化的参数配置。例如:当你第一个日期选定之后,那么第二个日期的minDate肯定就是从当前选定日期的下一天开始,2014-03-17[日期1],那么日期2就当然是从2014-03-18开始了。请看简单的代码:
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
4 <title>jquery-ui-datepicker</title>
5 <link rel="stylesheet" type="text/css" href="css/jquery-ui-1.10.4.custom.css" />
6 <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
7 <script type="text/javascript" src="js/jquery-ui-1.10.4.custom.js"></script>
8 <script type="text/javascript">
9 $(function(){
10 var obj;
11 $('#check_in').focusin(function(){
12 obj = $(this);
13 })
14
15 $('#check_out').focusin(function(sel_date){
16 obj = $(this);
17 })
18 //the common datepicker widget'code is here.
19 $('#datepicker').datepicker({
20 minDate : new Date(),
21 dateFormat : "yy-mm-dd",
22 onSelect : function(dateText,inst){
23 obj.val(dateText);
24 alert(inst)
25 }
26 });
27
28 })
29 </script>
30 </head>
31 <body>
32 <table>
33 <tr>
34 <td>CHECK IN</td>
35 <td>:</td>
36 <td><input type="text" name="check_in" id="check_in" /></td>
37 </tr>
38 <tr>
39 <td>CHECK OUT</td>
40 <td>:</td>
41 <td><input type="text" name="check_out" id="check_out" /></td>
42 </tr>
43 </table>
44 <div id="datepicker"></div>
45 </body>
46 </html>
比较完善的代码3:
默认输入的框为check_in,即使在用户没有点击check_in input输入框时 -> 当check_in输入框输入之后,自动跳转到check_out输入框 -> 再次点击日历,默认的输入框则变成为check_in。自动乱转。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>jquery-ui-datepicker</title>
<link rel="stylesheet" type="text/css" href="css/jquery-ui-1.10.4.custom.css" />
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.10.4.custom.js"></script>
<script type="text/javascript">
$(function(){
var obj;
$('#check_in').focusin(function(){
obj = $(this);
}) $('#check_out').focusin(function(sel_date){
obj = $(this);
}) $('#datepicker').datepicker({
minDate : new Date(),
dateFormat : "yy-mm-dd",
onSelect : function(dateText,inst){
if(!obj)
{
obj = $('#check_in');
}
obj.val(dateText);
if(obj.attr("name")=="check_in"){
$('#check_out').focusin();
}else{
$('#check_in').focusin();
}
}
}); })
</script>
</head>
<body>
<table>
<tr>
<td>CHECK IN</td>
<td>:</td>
<td><input type="text" name="check_in" id="check_in" /></td>
</tr>
<tr>
<td>CHECK OUT</td>
<td>:</td>
<td><input type="text" name="check_out" id="check_out" /></td>
</tr>
</table>
<div id="datepicker"></div>
</body>
</html>
代码4:实现check_in和check_out总是间隔一天,check_out不能选择小于check_in的日期。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>jquery ui widgets-datepicker</title>
<link type="text/css" rel="stylesheet" href="css/jquery-ui-1.10.3.custom.min.css" />
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.10.3.custom.min.js"></script>
<script type="text/javascript">
$(function(){
Array.prototype.in_array = function(e){
for(i=0;i<this.length;i++){
if(this[i] == e)
return true;
}
return false;
}
var no_room_date=new Array();
__(foreach from=$no_room_date key=i item=myday)__
no_room_date[__($i)__]="__($myday)__";
__(/foreach)__
var obj; $('#check_in').focusin(function(){
obj = $(this);
}) $('#check_out').focusin(function(sel_date){
obj = $(this);
}) $('#datepicker').datepicker({
minDate : new Date(),
maxDate : "__($maxdate)__",
dateFormat : "yy-mm-dd",
beforeShowDay:function(date){
var result=new Array();
result[0]="true";
result[1]="";
var nowmonth= parseInt(date.getMonth())+1;
var nowday=date.getDate();
if (nowmonth<10){
nowmonth="0"+nowmonth;
}
if(nowday<10){
nowday="0"+nowday;
}
var nowdate=date.getFullYear()+"-"+ nowmonth+"-"+nowday;
if(no_room_date.in_array(nowdate)){
result[0]=false;
}
if(nowdate==$("#check_in").val()){
result[1]="red"
}
if(nowdate==$("#check_out").val()){
result[1]="red"
}
if(nowdate>$("#check_in").val()&&nowdate<$("#check_out").val()&&(!no_room_date.in_array(nowdate))){
//alert(nowdate+" "+$("#check_in").val()+" "+$("#check_out").val());
result[1]="F06";
}
return result;
},
onSelect : function(dateText,inst){
if(!obj)
{
obj = $('#check_in');
$('#datepicker').datepicker('option','minDate',get_next_date(dateText));
}
obj.val(dateText);
if(obj.attr("name")=="check_in"){
$('#datepicker').datepicker('option','minDate',get_next_date(dateText));
$('#check_out').focusin();
}else{
$('#check_in').focusin();
$('#datepicker').datepicker("minDate" ,get_next_day($('#check_in').val())) }
}
});
})
function get_next_date(str_date){
var d="";
d=new Date(str_date);
d.setDate(d.getDate()+1);
var y=d.getFullYear();
var m=d.getMonth()+1;
var dd=d.getDate() < 10?'0'+parseInt(d.getDate()):parseInt(d.getDate());
return y+"-"+m+"-"+dd;
}
</script>
</head>
<body>
<table>
<tr>
<td>CHECK IN:</td>
<td><input type="text" name="check_in" id="check_in" value="2014-03-18" /></td>
</tr>
<tr>
<td>CHECK OUT:</td>
<td><input type="text" name="check_out" id="check_out" value="2014-03-26" /></td>
</tr>
</table>
<div id="datepicker"></div>
</body>
</html>







