日期选择器(Datepicker)


jQuery UI 实例 - 日期选择器(Datepicker)

从弹出框或内联日历选择一个日期。

如需了解更多有关 datepicker 部件的细节,请查看 API 文档 日期选择器部件(Datepicker Widget)。

默认功能

日期选择器(Datepicker)绑定到一个标准的表单 input 字段上。把焦点移到 input 上(点击或者使用 tab 键),在一个小的覆盖层上打开一个交互日历。选择一个日期,点击页面上的任意地方(输入框即失去焦点),或者点击 Esc 键来关闭。如果选择了一个日期,则反馈显示为 input 的值。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 默认功能</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker();
  13.   });
  14.   </script>
  15. </head>
  16. <body>
  17.  
  18. <p>日期:<input type="text" id="datepicker"></p>
  19.  
  20.  
  21. </body>
  22. </html>


动画

当打开或关闭 datepicker 时使用不同的动画。从下拉框中选择一个动画,然后在输入框中点击来查看它的效果。您可以使用三个标准动画中任意一个,或者使用 UI 特效中的任意一个。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 日期选择器(Datepicker) - 动画</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8. <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9. <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10. <script>
  11. $(function() {
  12. $( "#datepicker" ).datepicker();
  13. $( "#anim" ).change(function() {
  14. $( "#datepicker" ).datepicker( "option", "showAnim", $( this ).val() );
  15. });
  16. });
  17. </script>
  18. </head>
  19. <body>
  20.  
  21. <p>日期:<input type="text" id="datepicker" size="30"></p>
  22.  
  23. <p>动画:<br>
  24. <select id="anim">
  25. <option value="show">Show (默认)</option>
  26. <option value="slideDown">滑下</option>
  27. <option value="fadeIn">淡入</option>
  28. <option value="blind">Blind (UI 百叶窗特效)</option>
  29. <option value="bounce">Bounce (UI 反弹特效)</option>
  30. <option value="clip">Clip (UI 剪辑特效)</option>
  31. <option value="drop">Drop (UI 降落特效)</option>
  32. <option value="fold">Fold (UI 折叠特效)</option>
  33. <option value="slide">Slide (UI 滑动特效)</option>
  34. <option value=""></option>
  35. </select>
  36. </p>
  37.  
  38.  
  39. </body>
  40. </html>


其他月份的日期

datepicker 可以显示其他月份的日期,这些日期也可以设置成可选择的。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 其他月份的日期</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker({
  13.       showOtherMonths: true,
  14.       selectOtherMonths: true
  15.     });
  16.   });
  17.   </script>
  18. </head>
  19. <body>
  20.  
  21. <p>日期:<input type="text" id="datepicker"></p>
  22.  
  23.  
  24. </body>
  25. </html>


显示按钮栏

通过布尔值的 showButtonPanel 选项为选择当天日期显示一个"Today"按钮,为关闭日历显示一个"Done"按钮。默认情况下,当按钮栏显示时会启用每个按钮,但是按钮可通过其他的选项进行关闭。按钮文本可自定义。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 显示按钮栏</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker({
  13.       showButtonPanel: true
  14.     });
  15.   });
  16.   </script>
  17. </head>
  18. <body>
  19.  
  20. <p>日期:<input type="text" id="datepicker"></p>
  21.  
  22.  
  23. </body>
  24. </html>


内联显示

datepicker 是嵌套在页面中显示,而不是显示在一个覆盖层中。只需要简单地在 div 上调用 .datepicker() 即可,而不是在 input 上调用。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 内联显示</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker();
  13.   });
  14.   </script>
  15. </head>
  16. <body>
  17.  
  18. 日期:<div id="datepicker"></div>
  19.  
  20.  
  21. </body>
  22. </html>


显示月份 & 年份菜单

显示月份和年份的下拉框,而不是显示静态的月份/年份标题,这样便于在大范围的时间跨度上导航。添加布尔值 changeMonthchangeYear 选项即可。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 显示月份 &amp; 年份菜单</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker({
  13.       changeMonth: true,
  14.       changeYear: true
  15.     });
  16.   });
  17.   </script>
  18. </head>
  19. <body>
  20.  
  21. <p>日期:<input type="text" id="datepicker"></p>
  22.  
  23.  
  24. </body>
  25. </html>


显示多个月份

设置 numberOfMonths 选项为一个整数 2,或者大于 2 的整数,来在一个 datepicker 中显示多个月份。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 显示多个月份</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker({
  13.       numberOfMonths: 3,
  14.       showButtonPanel: true
  15.     });
  16.   });
  17.   </script>
  18. </head>
  19. <body>
  20.  
  21. <p>日期:<input type="text" id="datepicker"></p>
  22.  
  23.  
  24. </body>
  25. </html>


格式化日期

以各种方式显示日期反馈。从下拉框中选择一种日期格式,然后在输入框中点击并选择一个日期,查看所选格式的日期显示。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 格式化日期</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker();
  13.     $( "#format" ).change(function() {
  14.       $( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
  15.     });
  16.   });
  17.   </script>
  18. </head>
  19. <body>
  20.  
  21. <p>日期:<input type="text" id="datepicker" size="30"></p>
  22.  
  23. <p>格式选项:<br>
  24.   <select id="format">
  25.     <option value="mm/dd/yy">Default - mm/dd/yy</option>
  26.     <option value="yy-mm-dd">ISO 8601 - yy-mm-dd</option>
  27.     <option value="d M, y">Short - d M, y</option>
  28.     <option value="d MM, y">Medium - d MM, y</option>
  29.     <option value="DD, d MM, yy">Full - DD, d MM, yy</option>
  30.     <option value="&apos;day&apos; d &apos;of&apos; MM &apos;in the year&apos; yy">With text - 'day' d 'of' MM 'in the year' yy</option>
  31.   </select>
  32. </p>
  33.  
  34.  
  35. </body>
  36. </html>


图标触发器

点击输入框旁边的图标来显示 datepicker。设置 datepicker 在获得焦点时打开(默认行为),或者在点击图标时打开,或者在获得焦点/点击图标时打开。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 图标触发器</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker({
  13.       showOn: "button",
  14.       buttonImage: "images/calendar.gif",
  15.       buttonImageOnly: true
  16.     });
  17.   });
  18.   </script>
  19. </head>
  20. <body>
  21.  
  22. <p>日期:<input type="text" id="datepicker"></p>
  23.  
  24.  
  25. </body>
  26. </html>


本地化日历

本地化 datepicker 日历语言和格式(默认为 English / Western 格式)。datepicker 包含对从右到左读取的语言的内建支持,比如 Arabic 和 Hebrew。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 本地化日历</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <script src="http://jqueryui.com/resources/demos/datepicker/jquery.ui.datepicker-ar.js"></script>
  10.   <script src="http://jqueryui.com/resources/demos/datepicker/jquery.ui.datepicker-fr.js"></script>
  11.   <script src="http://jqueryui.com/resources/demos/datepicker/jquery.ui.datepicker-he.js"></script>
  12.   <script src="http://jqueryui.com/resources/demos/datepicker/jquery.ui.datepicker-zh-TW.js"></script>
  13.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  14.   <script>
  15.   $(function() {
  16.     $( "#datepicker" ).datepicker( $.datepicker.regional[ "fr" ] );
  17.     $( "#locale" ).change(function() {
  18.       $( "#datepicker" ).datepicker( "option",
  19.         $.datepicker.regional[ $( this ).val() ] );
  20.     });
  21.   });
  22.   </script>
  23. </head>
  24. <body>
  25.  
  26. <p>日期:<input type="text" id="datepicker">&nbsp;
  27.   <select id="locale">
  28.     <option value="ar">Arabic (&#8235;(&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;</option>
  29.     <option value="zh-TW">Chinese Traditional (&#32321;&#39636;&#20013;&#25991;)</option>
  30.     <option value="">English</option>
  31.     <option value="fr" selected="selected">French (Fran&ccedil;ais)</option>
  32.     <option value="he">Hebrew (&#8235;(&#1506;&#1489;&#1512;&#1497;&#1514;</option>
  33.   </select></p>
  34.  
  35.  
  36. </body>
  37. </html>


填充另一个输入框

使用 altFieldaltFormat 选项,无论何时选择日期,会在另一个输入框中填充带有一定格式的日期。这个功能通过对电脑友好性的日期进一步加工后,向用户呈现一个用户友好性的日期。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 填充另一个输入框</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker({
  13.       altField: "#alternate",
  14.       altFormat: "DD, d MM, yy"
  15.     });
  16.   });
  17.   </script>
  18. </head>
  19. <body>
  20.  
  21. <p>日期:<input type="text" id="datepicker">&nbsp;<input type="text" id="alternate" size="30"></p>
  22.  
  23.  
  24. </body>
  25. </html>


限制日期范围

通过 minDatemaxDate 选项限制可选择的日期范围。设置起止日期为实际的日期(new Date(2009, 1 - 1, 26)),或者为与今天的一个数值偏移(-20),或者为一个周期和单位的字符串('+1M +10D')。如果设置为字符串,使用 'D' 表示天,使用 'W' 表示周,使用 'M' 表示月,使用 'Y' 表示年。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 限制日期范围</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
  13.   });
  14.   </script>
  15. </head>
  16. <body>
  17.  
  18. <p>日期:<input type="text" id="datepicker"></p>
  19.  
  20.  
  21. </body>
  22. </html>


选择一个日期范围

选择要搜索的日期范围。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 选择一个日期范围</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#from" ).datepicker({
  13.       defaultDate: "+1w",
  14.       changeMonth: true,
  15.       numberOfMonths: 3,
  16.       onClose: function( selectedDate ) {
  17.         $( "#to" ).datepicker( "option", "minDate", selectedDate );
  18.       }
  19.     });
  20.     $( "#to" ).datepicker({
  21.       defaultDate: "+1w",
  22.       changeMonth: true,
  23.       numberOfMonths: 3,
  24.       onClose: function( selectedDate ) {
  25.         $( "#from" ).datepicker( "option", "maxDate", selectedDate );
  26.       }
  27.     });
  28.   });
  29.   </script>
  30. </head>
  31. <body>
  32.  
  33. <label for="from"></label>
  34. <input type="text" id="from" name="from">
  35. <label for="to"></label>
  36. <input type="text" id="to" name="to">
  37.  
  38.  
  39. </body>
  40. </html>


显示一年中的第几周

datepicker 可以显示一年中的第几周。默认的计算是按照 ISO 8601 定义:每周从星期一开始,每年的第一周包含该年的第一个星期四。这就意味着一年中的一些天可能是属于另一年中的周。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 日期选择器(Datepicker) - 显示一年中的第几周</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#datepicker" ).datepicker({
  13.       showWeek: true,
  14.       firstDay: 1
  15.     });
  16.   });
  17.   </script>
  18. </head>
  19. <body>
  20.  
  21. <p>日期:<input type="text" id="datepicker"></p>
  22.  
  23.  
  24. </body>
  25. </html>