当前位置: 首页 > 编程文章 >

jQuery Datepicker 参数详解与使用示例

jQuery Datepicker 参数详解与使用示例

作者:陆安时间:2025-01-30 14:28:24浏览:462

本文详细列举了 jQuery UI Datepicker 插件的核心配置参数,涵盖 altField、dateFormat、minDate 等属性设置,以及 beforeShow、onSelect 等事件回调。同时介绍了 formatDate 格式化规则、全局方法如 setDefaults 和 parseDate,帮助开发者快速定制符合需求的日期选择组件。

本文目录展开

jQuery Datepicker 参数详解与使用示例

复制代码 代码如下:


2.参数 复制代码 代码如下: 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 设为 true 时,将在输入框后放置一张图片作为触发器,而不将其渲染为按钮样式。 初始调用:$('.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 设为 true 允许用户通过下拉菜单列表选择月份。 初始调用:$('.selector').datepicker({ changeMonth: true }); 获取值:var changeMonth = $('.selector').datepicker('option', 'changeMonth'); 设置值:$('.selector').datepicker('option', 'changeMonth', true);

changeYear : Boolean : false 设为 true 允许用户通过下拉菜单列表选择年份。 初始调用:$('.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 时,限制用户输入的日期格式必须符合 dateFormat 设定。 初始调用:$('.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'] 设置一周中每天的极简缩写,从周日开始,用于日历表头显示。 初始调用:$('.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'] 设置一周中每天的短写形式,从周日开始,用于日历表头显示。 初始调用:$('.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 表示后天)或有效字符串(如 '+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 设为 true 时,若无上一个或下一个可用月份,则隐藏对应的导航按钮(默认为禁用状态)。 初始调用:$('.selector').datepicker({ hideIfNoPrevNext: true }); 获取值:var hideIfNoPrevNext = $('.selector').datepicker('option', 'hideIfNoPrevNext'); 设置值:$('.selector').datepicker('option', 'hideIfNoPrevNext', true);

isRTL : Boolean : false 设为 true 时,界面文字排版方向变为从右向左(Right-to-Left)。 初始调用:$('.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 对象、相对数字或有效字符串(如 '+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 时,prevText、nextText 和 currentText 的值将通过 formatDate 函数进行格式化,例如显示为月份名称。 初始调用:$('.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 设置一次显示的月份数量。整数表示单行显示的月数;数组 [rows, cols] 表示多行多列布局。 初始调用:$('.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 设为 true 时在面板底部显示操作按钮面板。 初始调用:$('.selector').datepicker({ showButtonPanel: true }); 获取值:var showButtonPanel = $('.selector').datepicker('option', 'showButtonPanel'); 设置值:$('.selector').datepicker('option', 'showButtonPanel', true);

showCurrentAtPos : Number : 0 在多月份显示模式下,设置当前月份在网格中的位置索引,从顶部/左侧开始计数。 初始调用:$('.selector').datepicker({ showCurrentAtPos: 3 }); 获取值:var showCurrentAtPos = $('.selector').datepicker('option', 'showCurrentAtPos'); 设置值:$('.selector').datepicker('option', 'showCurrentAtPos', 3);

showMonthAfterYear : Boolean : false 设为 true 时,在面板头部先显示年份,随后显示月份。 初始调用:$('.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 设为 true 时,在当前面板中显示上月和下月的部分日期(通常不可选),以填充日历网格。 初始调用:$('.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) { ... } });

$.datepicker.setDefaults( settings ) - 全局设置日期选择插件的默认参数。 $.datepicker.formatDate( format, date, settings ) - 根据指定格式将日期对象转换为字符串。 $.datepicker.iso8601Week( date ) - 计算给定日期是一年中的第几周。 $.datepicker.parseDate( format, value, settings ) - 按照指定格式解析日期字符串为 Date 对象。

d - 每月的第几天 (无前导零) dd - 每月的第几天 (两位数字) o - 一年中的第几天 (无前导零) oo - 一年中的第几天 (三位数字) D - 星期名称缩写 DD - 星期名称全称 m - 月份 (无前导零) mm - 月份 (两位数字) M - 月份名称缩写 MM - 月份名称全称 y - 年份 (两位数字) yy - 年份 (四位数字) @ - Unix 时间戳 (从 1970-01-01 起) '...' - 纯文本 '' - 单引号字符 (其他字符) - 直接输出文本

ATOM - 'yy-mm-dd' (等同于 RFC 3339/ISO 8601) COOKIE - 'D, dd M yy' ISO_8601 - 'yy-mm-dd' RFC_822 - 'D, d M y' RFC_850 - 'DD, dd-M-y' RFC_1036 - 'D, d M y' RFC_1123 - 'D, d M yy' RFC_2822 - 'D, d M yy' RSS - 'D, d M y' TIMESTAMP - '@' W3C - 'yy-mm-dd'

上一篇
详解JavaScript对象序列化

对象序列化是将对象状态转换为可存储或传输形式的过程,主要用于数据的持久化存储与网络传输。JavaScript通过内置的JSON.stringify()和JSON.parse()方法实现这一机制。前者将对象转为字符串,支持过滤、缩进及自定义toJSON逻辑;后者将字符串还原为对象,可通过reviver函数处理解析值。需注意JSON语法对undefined、函数等类型的限制,以及日期对象仅保留字符串形态的特性。

下一篇
jQuery前端实现列表模糊查询及性能优化分析

本文介绍利用jQuery在前端直接对异步返回的长列表进行模糊匹配显示的方法,无需后端再次请求。核心逻辑通过监听输入框事件,使用filter和contains筛选内容并控制DOM显隐。针对数据量大时引发的移动端DOM操作卡顿问题,文章分析了原因并探讨了可能的优化方向。

相关文章

更多
PHP日期格式统一与时间戳计算技巧

本文总结了PHP中两个实用的日期操作技巧。首先介绍如何利用checkdate和mktime函数将非标准格式的日期(如2026-9-9)转换为数据库通用的Y-m-d格式,确保存储一致性。其次讲解如何避免手动计算日期时遇到的月末、闰月等复杂逻辑,通过strtotime("-1 day")直接获取上一天的开始和结束时间戳,利用PHP内置算法自动处理月份天数差异。

2026.04.26 990
PHP使用date()函数获取并格式化当前日期时间详解

在PHP开发中,利用内置的date()函数可以便捷地获取当前系统时间并进行自定义格式化输出。该函数通过特定的格式字符(如Y、m、d、H、i、s等)来定义年、月、日、时、分、秒的显示样式,支持12小时制与24小时制切换,以及星期和月份的英文全称或缩写形式。掌握这些参数组合,能够灵活生成符合业务需求的日期时间字符串。

2026.08.18 948
PHP文章按日期归档的SQL语句示例

本文提供PHP中实现文章按日期归档的SQL查询语句。针对时间戳字段,可使用FROM_UNIXTIME函数按月(%Y-%m)或按日(%Y-%m-%d)进行分组统计;针对非时间戳格式日期,可利用date_format函数格式化后分组,并支持自定义显示格式及排序限制,适用于WordPress等数据库环境。

2025.02.27 911
PHP结合jQuery UI Autocomplete实现输入自动提示功能

本文介绍如何利用jQuery UI的autocomplete插件配合后端PHP脚本,实现类似搜索引擎的输入框自动提示功能。通过读取MySQL数据库中的内容并以JSON格式返回,前端在用户输入指定字符数后动态展示匹配结果,从而提升交互体验。文中还包含了插件参数配置、数据库表结构及处理Firefox中文输入Bug的具体代码方案。

2026.09.14 824
PHP获取时间戳与日期格式转换及常用时间段计算

本文介绍PHP中时间戳与日期格式的相互转换方法,重点讲解date()和strtotime()函数的参数用法。通过示例代码演示如何获取当前、昨日、本周、上周、本月及本年的时间戳和具体日期范围,并强调在使用时间函数时需正确配置时区以避免偏差。

2025.07.23 810
PHP日期加减运算:基于时间戳的实现方案

PHP未直接提供标准日期格式的时间加减功能,需借助时间戳实现。通过将原始日期转换为Unix时间戳,进行数值运算后再转回日期格式,可完成精确到秒的日期计算。本文以2012年4月25日加5个月为例,演示利用date()与strtotime()函数的具体操作步骤。

2026.02.05 709

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。