JqueryUI - 日期选择器


jQueryUI 中的日期选择器允许用户轻松直观地输入日期。您可以自定义日期格式和语言,限制可选择的日期范围并轻松添加按钮和其他导航选项。

jQueryUI 提供了datepicker()方法,该方法创建日期选择器并通过添加新的 CSS 类来更改页面上 HTML 元素的外观。将包装集中的 <input>、<div> 和 <span> 元素转换为日期选择器控件。

默认情况下,对于 <input> 元素,当关联的文本字段获得焦点时,日期选择器日历会在一个小的覆盖层中打开。对于内联日历,只需将日期选择器附加到 <div> 或 <span> 元素即可。

句法

datepicker ()方法可以以两种形式使用 -

$(selector, context).datepicker(options) 方法

datepicker (options)方法声明 <input> 元素(或 <div> 或 <span>,具体取决于您选择显示日历的方式)应作为日期选择器进行管理。options参数是一个指定日期选择器元素Behave和外观的对象。

句法

$(selector, context).datepicker(options);

您可以使用 Javascript 对象一次提供一个或多个选项。如果要提供多个选项,那么您将使用逗号分隔它们,如下所示 -

$(selector, context).datepicker({option1: value1, option2: value2..... });

下表列出了可以与此方法一起使用的不同选项-

先生。 选项和说明
1 替代字段

此选项为字段指定一个 jQuery 选择器,该字段也会随任何日期选择而更新。altFormat选项可用于设置该值的格式。这对于将日期值设置到要提交到服务器的隐藏输入元素中非常有用,同时向用户显示更用户友好的格式。默认情况下它的值为""

选项 - altField

此选项为字段指定一个 jQuery 选择器,该字段也会随任何日期选择而更新。altFormat选项可用于设置该值的格式。这对于将日期值设置到要提交到服务器的隐藏输入元素中非常有用,同时向用户显示更用户友好的格式。默认情况下它的值为""

句法

$(".selector").datepicker(
   { altField: "#actualDate" }
);
2 替代格式

当指定altField选项时使用此选项。它提供要写入替代元素的值的格式。默认情况下它的值为""

选项 - altFormat

当指定altField选项时使用此选项。它提供要写入替代元素的值的格式。默认情况下它的值为""

句法

$(".selector").datepicker(
   { altFormat: "yy-mm-dd" }
);
3 附加文本

此选项是放置在 <input> 元素之后的字符串值,旨在向用户显示说明。默认情况下它的值为""

选项 - 附加文本

此选项是放置在 <input> 元素之后的字符串值,旨在向用户显示说明。默认情况下它的值为""

句法

$(".selector").datepicker(
   { appendText: "(yyyy-mm-dd)" }
);
4 自动尺寸

当设置为true时,此选项会调整 <input> 元素的大小,以适应使用 dateFormat 选项设置的日期选择器的日期格式。默认情况下它的值为false

选项 - 自动调整大小

This option when set to true resizes the <input> element to accommodate the datepicker’s date format as set with the dateFormat option. By default its value is false.

Syntax

$(".selector").datepicker(
   { autoSize: true }
);
5 演出前

此选项是一个回调函数,在显示日期选择器之前调用,并将 <input> 元素和日期选择器实例作为参数传递。此函数可以返回用于修改日期选择器的选项哈希。默认情况下它的值为""

Option - beforeShow

This option is a callback function that’s invoked just before a datepicker is displayed, with the <input> element and datepicker instance passed as parameters. This function can return an options hash used to modify the datepicker. By default its value is "".

6 演出日之前

此选项是一个回调函数,它采用日期作为参数,在日期选择器中的每一天显示之前调用该函数,并将日期作为唯一参数传递。这可用于覆盖日期元素的一些默认Behave。该函数必须返回一个三元素数组。默认情况下其值为null

Option - beforeShowDay

This option is a callback function which takes a date as parameter, that’s invoked for each day in the datepicker just before it’s displayed, with the date passed as the only parameter. This can be used to override some of the default behavior of the day elements. This function must return a three-element array, as follows −

  • [0]—true to make the date selectable, false otherwise.

  • [1]—A space-delimited string of CSS class names to be applied or an empty string to apply none

  • [2]—An optional string to apply a tooltip to the day element

By default its value is null.

7 按钮图像

此选项指定要在通过将showOn选项设置为其中一个按钮或两个按钮而启用的按钮上显示的图像的路径。如果还提供了buttonText,则按钮文本将成为按钮的alt属性。默认情况下它的值为""

Option - buttonImage

This option specifies the path to an image to be displayed on the button enabled by setting the showOn option to one of buttons or both. If buttonText is also provided, the button text becomes the alt attribute of the button. By default its value is "".

Syntax

$(".selector").datepicker(
   { buttonImage: "/images/datepicker.gif" }
);
8 仅按钮图像

如果此选项设置为true,则指定 buttonImage 指定的图像将独立显示(不在按钮上)。showOn 选项仍必须设置为其中一个按钮或两个按钮才能显示图像。默认情况下它的值为false

Option - buttonImageOnly

This option if set to true, specifies that the image specified by buttonImage is to appear standalone (not on a button). The showOn option must still be set to one of button or both for the image to appear. By default its value is false.

Syntax

$(".selector").datepicker(
   { buttonImageOnly: true }
);
9 按钮文本

此选项指定通过将showOn选项设置为button之一或两者来启用的按钮的标题。默认情况下,其值为“...”

Option - buttonText

This option specifies the caption for the button enabled by setting the showOn option to one of button or both. By default its value is "...".

Syntax

$(".selector").datepicker(
   { buttonText: "Choose" }
);
10 计算周

此选项是一个自定义函数,用于计算并返回作为单独参数传递的日期的周数。默认实现是由$.datepicker.iso8601Week()实用函数提供的。

Option - calculateWeek

This option is a custom function to calculate and return the week number for a date passed as the lone parameter. The default implementation is that provided by the $.datepicker.iso8601Week() utility function.

Syntax

$(".selector").datepicker(
   { calculateWeek: myWeekCalc }
);
11 改变月份

如果此选项设置为true,则会显示月份下拉列表,允许用户直接更改月份,而无需使用箭头按钮逐步浏览。默认情况下它的值为false

Option - changeMonth

This option if set to true, a month dropdown is displayed, allowing the user to directly change the month without using the arrow buttons to step through them. By default its value is false.

Syntax

$(".selector").datepicker(
   { changeMonth: true }
);
12 更改年份

如果此选项设置为true,则会显示年份下拉列表,允许用户直接更改年份,而无需使用箭头按钮逐步浏览。选项yearRange可用于控制可供选择的年份。默认情况下它的值为false

Option - changeYear

This option if set to true, a year dropdown is displayed, allowing the user to directly change the year without using the arrow buttons to step through them. Option yearRange can be used to control which years are made available for selection. By default its value is false.

Syntax

$(".selector").datepicker(
   { changeYear: true }
);
13 关闭文本

此选项指定用于替换关闭按钮的默认标题“完成”的文本。当通过showButtonPanel选项显示按钮面板时使用它。默认情况下,其值为“完成”

Option - closeText

This option specifies the text to replace the default caption of Done for the close button. It is used when the button panel is displayed via the showButtonPanel option. By default its value is "Done".

Syntax

$(".selector").datepicker(
   { closeText: "Close" }
);
14 约束输入

如果此选项设置为true(默认值),则 <input> 元素中的文本输入将限制为当前dateformat选项允许的字符。默认情况下它的值为true

Option - constrainInput

This option if set true (the default), text entry into the <input> element is constrained to characters allowed by the current dateformat option. By default its value is true.

Syntax

$(".selector").datepicker(
   { constrainInput: false }
);
15 当前文本

此选项指定用于替换当前按钮今天默认标题的文本。如果通过showButtonPanel选项显示按钮面板,则使用此选项。默认情况下,其值为Today

Option - currentText

This option specifies the text to replace the default caption of Today for the current button. This is used if the button panel is displayed via the showButtonPanel option. By default its value is Today.

Syntax

$(".selector").datepicker(
   { currentText: "Now" }
);
16 日期格式

该选项指定要使用的日期格式。默认情况下,其值为mm/dd/yy

Option - dateFormat

This option specifies the date format to be used. By default its value is mm/dd/yy.

Syntax

$(".selector").datepicker(
   { dateFormat: "yy-mm-dd" }
);
17 号 日期名称

此选项是一个 7 元素数组,提供全天名称,第 0 个元素代表星期日。可用于本地化控制。默认情况下,其值为[ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]

Option - dayNames

This option is a 7-element array providing the full day names with the 0th element representing Sunday. Can be used to localize the control. By default its value is [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ].

Syntax

$(".selector").datepicker(
   { dayNames: [ "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag" ] }
);
18 日期名称分钟

此选项是一个 7 元素数组,提供最小日期名称,第 0 个元素代表星期日,用作列标题。可用于本地化控制。默认情况下,其值为[ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" ]

Option - dayNamesMin

This option is a 7-element array providing the minimal day names with the 0th element representing Sunday, used as column headers. Can be used to localize the control. By default its value is [ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" ].

Syntax

$(".selector").datepicker(
   { dayNamesMin: [ "So", "Mo", "Di", "Mi", "Do", "Fr", "Sa" ] }
);
19 日名短

此选项指定一个 7 元素数组,提供短日名称,其中第 0 个元素代表星期日。可用于本地化控制。默认情况下,其值为[ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ]

Option - dayNamesShort

This option specifies a 7-element array providing the short day names with the 0th element representing Sunday. Can be used to localize the control. By default its value is [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ].

Syntax

$(".selector").datepicker(
   { dayNamesShort: [ "Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam" ] }
);
20 默认日期

如果 <input> 元素没有值,此选项设置控件的初始日期,覆盖今天的默认值。这可以是Date实例、从今天算起的天或指定绝对或相对日期的字符串。默认情况下它的值为null

Option - defaultDate

This option sets the initial date for the control, overriding the default value of today, if the <input> element has no value. This can be a Date instance, the number of days from today, or a string specifying an absolute or relative date. By default its value is null.

Syntax

$(".selector").datepicker(
   { defaultDate: +7 }
);
21 期间

此选项指定日期选择器出现的动画速度。可以是慢速、正常或快速之一,也可以是动画跨越的毫秒数。默认情况下其值为正常

Option - duration

This option specifies the speed of the animation that makes the datepicker appear. Can be one of slow, normal, or fast, or the number of milliseconds for the animation to span. By default its value is normal.

Syntax

$(".selector").datepicker(
   { duration: "slow" }
);
22 第一天

此选项指定哪一天被视为一周的第一天,并将显示为最左边的列。默认情况下其值为0

Option - firstDay

This option specifies which day is considered the first day of the week, and will be displayed as the left-most column. By default its value is 0.

Syntax

$(".selector").datepicker(
   { firstDay: 1 }
);
23 转到当前

当此选项设置为true时,当天链接将设置为所选日期,覆盖今天的默认值。默认情况下它的值为false

Option - gotoCurrent

This option when set to true, the current day link is set to the selected date, overriding the default of today. By default its value is false.

Syntax

$(".selector").datepicker(
   { gotoCurrent: true }
);
24 隐藏如果没有上一页下一页

如果此选项设置为true,则当下一个和上一个链接不适用时会隐藏它们(而不是仅仅禁用它们),具体取决于minDatemaxDate选项的设置。默认情况下它的值为false

Option - hideIfNoPrevNext

This option if set to true, hides the next and previous links (as opposed to merely disabling them) when they aren’t applicable, as determined by the settings of the minDate and maxDate options. By default its value is false.

Syntax

$(".selector").datepicker(
   { hideIfNoPrevNext: true }
);
25 是RTL

当此选项设置为true时,将本地化指定为从右到左的语言。默认情况下它的值为false

Option - isRTL

This option when set to true, specifies the localizations as a right-to-left language. By default its value is false.

Syntax

$(".selector").datepicker(
   { isRTL: true }
);
26 最大日期

此选项设置控件的最大可选日期。这可以是 Date 实例、从今天算起的天数或指定绝对或相对日期的字符串。默认情况下它的值为null

Option - maxDate

This option sets the maximum selectable date for the control. This can be a Date instance, the number of days from today, or a string specifying an absolute or relative date. By default its value is null.

Syntax

$(".selector").datepicker(
   { maxDate: "+1m +1w" }
);
27 最短日期

此选项设置控件的最小可选日期。这可以是Date实例、从今天算起的天或指定绝对或相对日期的字符串。默认情况下它的值为null

Option - minDate

This option sets the minimum selectable date for the control. This can be a Date instance, the number of days from today, or a string specifying an absolute or relative date. By default its value is null.

Syntax

$(".selector").datepicker(
   { minDate: new Date(2007, 1 - 1, 1) }
);
28 月份名称

此选项是一个 12 元素数组,提供完整的月份名称,其中第 0 个元素代表一月。可用于本地化控制。默认情况下,其值为[ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", 《十二月》] .

Option - monthNames

This option is a 12-element array providing the full month names with the 0th element representing January. Can be used to localize the control. By default its value is [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ].

Syntax

$(".selector").datepicker(
   { monthNames: [ "janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre" ]  }
);
29 月名短

此选项指定一个 12 元素数组,提供短月份名称,其中第 0 个元素代表一月。可用于本地化控制。默认情况下,其值为[ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", “十二月”]

Option - monthNamesShort

This option specifies a 12-element array providing the short month names with the 0th element representing January. Can be used to localize the control. By default its value is [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ].

Syntax

$(".selector").datepicker(
   { monthNamesShort: [ "jan", "fév", "mar", "avr", "mai", "Jui", "Jul", "aoû", "sep", "Oot", "nov", "déc" ] }
);
30 导航为日期格式

如果此选项设置为true ,则 nextText、prevTextcurrentText的导航链接将在显示之前通过$.datepicker.formatDate()函数传递 。这允许为那些被相关值替换的选项提供日期格式。默认情况下它的值为false

Option - navigationAsDateFormat

This option if set to true, the navigation links for nextText, prevText, and currentText are passed through the $.datepicker.formatDate() function prior to display. This allows date formats to be supplied for those options that get replaced with the relevant values. By default its value is false.

Syntax

$(".selector").datepicker(
   { navigationAsDateFormat: true }
);
31 下一个文本

此选项指定用于替换下个月导航链接的“下一个”默认标题的文本。ThemeRoller 将此文本替换为图标。默认情况下,其值为Next

Option - nextText

This option specifies the text to replace the default caption of Next for the next month navigation link. ThemeRoller replaces this text with an icon. By default its value is Next.

Syntax

$(".selector").datepicker(
   {  nextText: "Later" }
);
32 月数

此选项指定日期选择器中显示的月数。默认情况下其值为1

Option - numberOfMonths

This option specifies the number of months to show in the datepicker. By default its value is 1. Multiple types supported −

  • Number − The number of months to display in a single row.

  • Array − An array defining the number of rows and columns to display.

Syntax

$(".selector").datepicker(
   { numberOfMonths: [ 2, 3 ] }
);
33 更改月年

此选项是当日期选择器移动到新的月份或年份时调用的回调,其中选定的年、月(从 1 开始)和日期选择器实例作为参数传递,并且函数上下文设置为输入字段元素。默认情况下它的值为null

Option - onChangeMonthYear

This option is a callback that’s invoked when the datepicker moves to a new month or year, with the selected year, month (1-based), and datepicker instance passed as parameters, and the function context is set to the input field element. By default its value is null.

34 关闭时

此选项是每当日期选择器关闭时调用的回调,将选定的日期作为文本传递(如果没有选择,则为空字符串),以及日期选择器实例,并将函数上下文设置为输入字段元素。默认情况下它的值为null

Option - onClose

This option is a callback invoked whenever a datepicker is closed, passed the selected date as text (the empty string if there is no selection), and the datepicker instance, and the function context is set to the input field element. By default its value is null.

35 选择时

此选项是每当选择日期时调用的回调,将所选日期作为文本传递(如果没有选择则为空字符串)和日期选择器实例,并且函数上下文设置为输入字段元素。默认情况下它的值为null

Option - onSelect

This option is a callback invoked whenever a date is selected, passed the selected date as text (the empty string if there is no selection), and the datepicker instance, and the function context is set to the input field element. By default its value is null.

36 上一篇

此选项指定用于替换上个月导航链接的Prev默认标题的文本。(请注意,ThemeRoller 将此文本替换为图标)。默认情况下,其值为PrevdefaultDatedayNamesMin

Option - prevText

This option specifies the text to replace the default caption of Prev for the previous month navigation link. (Note that the ThemeRoller replaces this text with an icon). By default its value is Prev.

Syntax

$(".selector").datepicker(
   { prevText: "Earlier" }
);
37 选择其他月份

如果此选项设置为true,则可以选择显示的月份之前或之后显示的天数。除非showOtherMonths选项为 true ,否则不会显示此类日期。默认情况下它的值为false

Option - selectOtherMonths

This option if set to true, days shown before or after the displayed month(s) are selectable. Such days aren’t displayed unless the showOtherMonths option is true. By default its value is false.

Syntax

$(".selector").datepicker(
   { selectOtherMonths: true }
);
38 短期截止

如果此选项是数字,则指定 0 到 99 年之间的值,在此之前的任何 2 位年份值都将被视为属于上一个世纪。如果此选项是字符串,则该值将进行数字转换并添加到当前年份。默认值为+10,表示从当前年份起 10 年。

Option - shortYearCutoff

This option if its a number, specifies a value between 0 and 99 years before which any 2-digit year values will be considered to belong to the previous century. If this option is a string, the value undergoes a numeric conversion and is added to the current year. The default is +10 which represents 10 years from the current year.

Syntax

$(".selector").datepicker(
   { shortYearCutoff: 50 }
);
39 显示动画

此选项指定设置用于显示和隐藏日期选择器的动画的名称。如果指定,则必须是show(默认)、fadeIn、slideDown或任何 jQuery UI 显示/隐藏动画之一。默认情况下其值为show

Option - showAnim

This option specifies sets the name of the animation to be used to show and hide the datepicker. If specified, must be one of show (the default), fadeIn, slideDown, or any of the jQuery UI show/hide animations. By default its value is show.

Syntax

$(".selector").datepicker(
   { showAnim: "fold" }
);
40 显示按钮面板

如果将此选项设置为true,则会在日期选择器底部显示一个按钮面板,其中包含当前按钮和关闭按钮。这些按钮的标题可以通过currentTextcloseText选项提供。默认情况下它的值为false

Option - showButtonPanel

This option if set to true, a button panel at the bottom of the datepicker is displayed, containing current and close buttons. The caption of these buttons can be provided via the currentText and closeText options. By default its value is false.

Syntax

$(".selector").datepicker(
   { showButtonPanel: true }
);
41 显示当前位置

此选项指定从左上角开始的从 0 开始的索引,包含当前日期的月份应放置在多月显示中的位置。默认情况下其值为0

Option - showCurrentAtPos

This option specifies the 0-based index, starting at the upper left, of where the month containing the current date should be placed within a multi-month display. By default its value is 0.

Syntax

$(".selector").datepicker(
   { showCurrentAtPos: 3 }
);
42 显示年复一年

此选项指定如果设置为true,则月份和年份在日期选择器标题中的位置会颠倒。默认情况下它的值为false

Option - showMonthAfterYear

This option specifies if set to true, the positions of the month and year are reversed in the header of the datepicker. By default its value is false.

Syntax

$(".selector").datepicker(
   { showMonthAfterYear: true }
);
43 表演

此选项指定日期选择器应何时出现。可能的值为focus、button 或两者。默认情况下它的值为focus

Option - showOn

This option specifies when the datepicker should appear. The possible values are focus, button or both. By default its value is focus.

focus (default) causes the datepicker to display when the <input> element gains focus.

button causes a button to be created after the <input> element (but before any appended text) that triggers the datepicker when clicked.

both causes the trigger button to be created, and for focus events to also trigger the datepicker.

Syntax

$(".selector").datepicker(
   { showOn: "both" }
);
44 显示选项

当为showAnim选项指定 jQuery UI 动画时,此选项提供要传递给动画的哈希值。默认情况下,其值为{}

Option - showOptions

This option provides a hash to be passed to the animation when a jQuery UI animation is specified for the showAnim option. By default its value is {}.

Syntax

$(".selector").datepicker(
   { showOptions: { direction: "up" } }
);
45 显示其他月份

如果此选项设置为true,则显示当月第一天和最后一天之前或之后的日期。除非 selectOtherMonths 选项也设置为 true,否则这些日期不可选。默认情况下它的值为false

Option - showOtherMonths

This option if set to true, dates before or after the first and last days of the current month are displayed. These dates aren't selectable unless the selectOtherMonths option is also set to true. By default its value is false.

Syntax

$(".selector").datepicker(
   { showOtherMonths: true }
);
46 表演周

如果此选项设置为true,周数将显示在月份显示左侧的列中。calculateWeek 选项可用于更改确定该值的方式。默认情况下它的值为false

Option - showWeek

This option if set to true, the week number is displayed in a column to the left of the month display. The calculateWeek option can be used to alter the manner in which this value is determined. By default its value is false.

Syntax

$(".selector").datepicker(
   { showWeek: true }
);
47 步数月

此选项指定单击月份导航控件之一时要移动的月份数。默认情况下其值为1

Option - stepMonths

This option specifies specifies how many months to move when one of the month navigation controls is clicked. By default its value is 1.

Syntax

$(".selector").datepicker(
   { stepMonths: 3 }
);
48 周标题

此选项指定要在周数列中显示的文本,当 showWeek 为 true 时,将覆盖 Wk 的默认值。默认情况下,其值为Wk

Option - weekHeader

This option specifies the text to display for the week number column, overriding the default value of Wk, when showWeek is true. By default its value is Wk.

Syntax

$(".selector").datepicker(
   { weekHeader: "W" }
);
49 年份范围

此选项指定当changeYeartrue时以from:to形式在下拉列表中显示的年份的限制。这些值可以是绝对值或相对值(例如:2005:+2,表示 2005 年到 2 年后)。前缀 c 可用于使相对值相对于所选年份而不是当前年份进行偏移(例如:c-2:c+3)。默认情况下,其值为c-10:c+10

Option - yearRange

This option specifies limits on which years are displayed in the dropdown in the form from:to when changeYear is true. The values can be absolute or relative (for example: 2005:+2, for 2005 through 2 years from now). The prefix c can be used to make relative values offset from the selected year rather than the current year (example: c-2:c+3). By default its value is c-10:c+10.

Syntax

$(".selector").datepicker(
   { yearRange: "2002:2012" }
);
50 年后缀

此选项在日期选择器标题中的年份之后显示附加文本。默认情况下它的值为""

Option - yearSuffix

This option displays additional text after the year in the datepicker header. By default its value is "".

Syntax

$(".selector").datepicker(
   { yearSuffix: "CE" }
);

以下部分将向您展示日期选择器功能的一些工作示例。

默认功能

以下示例演示了 datepicker 功能的简单示例,不向datepicker()方法传递任何参数。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

      <!-- Javascript -->
      <script>
         $(function() {
            $( "#datepicker-1" ).datepicker();
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <p>Enter Date: <input type = "text" id = "datepicker-1"></p>
   </body>
</html>

让我们将上面的代码保存在 HTML 文件datepickerexample.htm中,并在支持 javascript 的标准浏览器中打开它,您还必须看到以下输出。现在,您可以使用结果 -

内联日期选择器

以下示例演示了内联日期选择器功能的简单示例。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#datepicker-2" ).datepicker();
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      Enter Date: <div id = "datepicker-2"></div>
   </body>
</html>

让我们将上面的代码保存在 HTML 文件datepickerexample.htm中,并在支持 javascript 的标准浏览器中打开它,您还必须看到以下输出。现在,您可以使用结果 -

在上面的示例中,我们使用 <div> 元素来获取内联日期选择器。

使用appendText、dateFormat、altField 和altFormat

下面的例子展示了JqueryUI的datepicker函数中三个重要选项(a)appendText(b)dateFormat (c)altField(d)altFormat的用法。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#datepicker-3" ).datepicker({
               appendText:"(yy-mm-dd)",
               dateFormat:"yy-mm-dd",
               altField: "#datepicker-4",
               altFormat: "DD, d MM, yy"
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <p>Enter Date: <input type = "text" id = "datepicker-3"></p>
      <p>Alternate Date: <input type = "text" id = "datepicker-4"></p>
   </body>
</html>

让我们将上面的代码保存在 HTML 文件datepickerexample.htm中,并在支持 javascript 的标准浏览器中打开它,您还必须看到以下输出。现在,您可以使用结果 -

在上面的示例中,您可以看到第一个输入的日期格式设置为yy-mm-dd。如果您从日期选择器中选择某个日期,则相同的日期将反映在第二个输入字段中,其日期格式设置为“DD,d MM,yy”。

beforeShowDay 的使用

以下示例显示了JqueryUI 的 datepicker 函数中选项beforeShowDay的用法。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

      <!-- Javascript -->
      <script>
         $(function() {
            $( "#datepicker-5" ).datepicker({
               beforeShowDay : function (date) {
                  var dayOfWeek = date.getDay ();
                  // 0 : Sunday, 1 : Monday, ...
                  if (dayOfWeek == 0 || dayOfWeek == 6) return [false];
                  else return [true];
               }
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <p>Enter Date: <input type = "text" id = "datepicker-5"></p>
   </body>
</html>

让我们将上面的代码保存在 HTML 文件datepickerexample.htm中,并在支持 javascript 的标准浏览器中打开它,您还必须看到以下输出。现在,您可以使用结果 -

在上面的示例中,星期日和星期六被禁用。

showOn、buttonImage 和 buttonImageOnly 的使用

下面的示例展示了JqueryUI 的 datepicker 函数中三个重要选项(a)showOn(b)buttonImage(c)buttonImageOnly的用法。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
   
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#datepicker-6" ).datepicker({
               showOn:"button",
               buttonImage: "/jqueryui/images/calendar-icon.png",
               buttonImageOnly: true
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <p>Enter Date: <input type = "text" id = "datepicker-6"></p>
   </body>
</html>

让我们将上面的代码保存在 HTML 文件datepickerexample.htm中,并在支持 javascript 的标准浏览器中打开它,您还必须看到以下输出。现在,您可以使用结果 -

在上面的示例中,显示了一个图标,需要单击该图标才能打开日期选择器。

使用 defaultDate、dayNamesMin 和持续时间

以下示例显示了JqueryUI 的 datepicker 函数中三个重要选项(a) dayNamesMin (b) dayNamesMin(c)uration的用法。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#datepicker-7" ).datepicker({
               defaultDate:+9,
               dayNamesMin: [ "So", "Mo", "Di", "Mi", "Do", "Fr", "Sa" ],
               duration: "slow"
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <p>Enter Date: <input type = "text" id = "datepicker-7"></p>
   </body>
</html>

让我们将上面的代码保存在 HTML 文件datepickerexample.htm中,并在支持 javascript 的标准浏览器中打开它,您还必须看到以下输出。现在,您可以使用结果 -

在上面的示例中,使用dayNamesMin更改日期的名称。您还可以看到设置了默认日期。

使用 prevText、nextText、showOtherMonths 和 selectOtherMonths

下面的示例显示了JqueryUI 的 datepicker 函数中三个重要选项(a) prevText (b) nextText (c) showOtherMonths(d) selectOtherMonths的用法。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      <!-- Javascript -->
      
      <script>
         $(function() {
            $( "#datepicker-8" ).datepicker({
               prevText:"click for previous months",
               nextText:"click for next months",
               showOtherMonths:true,
               selectOtherMonths: false
            });
            $( "#datepicker-9" ).datepicker({
               prevText:"click for previous months",
               nextText:"click for next months",
               showOtherMonths:true,
               selectOtherMonths: true
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <p>Enter Start Date: <input type = "text" id = "datepicker-8"></p>
      <p>Enter End Date: <input type = "text" id = "datepicker-9"></p>
   </body>
</html>

让我们将上面的代码保存在 HTML 文件datepickerexample.htm中,并在支持 javascript 的标准浏览器中打开它,您还必须看到以下输出。现在,您可以使用结果 -

在上面的示例中,prev 和 nec 链接都有标题。如果单击该元素,则会打开日期选择器。现在,在第一个日期选择器中,其他月份日期被禁用,因为 selectOtherMonths 设置为false。在第二个输入类型的第二个日期选择器中, selectOtherMonths 设置为true

使用changeMonth、changeYear 和numberOfMonths

下面的示例展示了JqueryUI 的 datepicker 函数中三个重要选项(a)changeMonth、b)changeYear(c)numberOfMonths的用法。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#datepicker-10" ).datepicker({
               changeMonth:true,
               changeYear:true,
               numberOfMonths:[2,2]
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <p>Enter Date: <input type = "text" id = "datepicker-10"></p>
   </body>
</html>

让我们将上面的代码保存在 HTML 文件datepickerexample.htm中,并在支持 javascript 的标准浏览器中打开它,您还必须看到以下输出。现在,您可以使用结果 -

在上面的示例中,您可以看到月份和年份字段的下拉菜单。我们以 [2,2] 的数组结构显示 4 个月。

showWeek、yearSuffix 和 showAnim 的使用

下面的示例展示了JqueryUI 的 datepicker 函数中三个重要选项(a)showWeek(b)yearSuffix(c)showAnim的用法。

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#datepicker-11" ).datepicker({
               showWeek:true,
               yearSuffix:"-CE",
               showAnim: "slide"
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <p>Enter Date: <input type = "text" id = "datepicker-11"></p>
   </body>
</html>

让我们将上面的代码保存在 HTML 文件datepickerexample.htm中,并在支持 javascript 的标准浏览器中打开它,您还必须看到以下输出。现在,您可以使用结果 -

在上面的示例中,您可以看到当showWeek设置为true时,周数显示在 datepicker 的左侧。年份有后缀“-CE”。

$(selector, context).datepicker("action", [params]) 方法

datepicker (action, params)方法可以对日历执行操作,例如选择新日期。该操作在第一个参数中指定为字符串,并且可以根据给定的操作提供一个或多个参数(可选)。

基本上,这里的操作什么都不是,只是我们可以以字符串形式使用的 jQuery 方法。

句法

$(selector, context).datepicker ("action", [params]);

下表列出了此方法的操作 -

先生。 动作与描述
1 破坏()

此操作完全删除了日期选择器功能。这将使元素返回到其预初始化状态。此方法不接受任何参数。

Action - destroy()

This action removes the datepicker functionality completely. This will return the element back to its pre-init state. This method does not accept any arguments.

Syntax

$(".selector").datepicker("destroy");
2 对话框(日期[,onSelect][,设置][,位置])

此操作在 jQuery UI 对话框中显示日期选择器。

Act