📜  jQuery UI Datepicker

📅  最后修改于: 2020-11-27 01:56:56             🧑  作者: Mango

jQuery UI日期选择器

jQuery UI datepicker小部件可帮助用户轻松直观地输入日期。用户可以自定义日期格式和语言,限制可选日期范围,轻松添加按钮和其他导航选项。

jQuery UI datepicker()方法创建一个日期选择器,并通过添加新的CSS类来更改页面上HTML元素的外观。

句法:

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

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

第一种方法

datepicker(选项)方法指定元素(或

,取决于您选择显示日历的方式)应作为日期选择器进行管理。 options参数指定datepicker元素的行为和外观。

句法:

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

您可以使用JavaScript对象一次使用一个或多个选项。如果有多个选项,则应使用逗号将它们分开,如下所示:

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

以下是可与该方法一起使用的不同选项的列表。

Option Description
altField This option specifies a jQuery selector for a field that is also updated with any date selections. The altFormat option can be used to set the format for this value. This is quite useful for setting date values into a hidden input element to be submitted to the server, while displaying a more user-friendly format to the user. By default its value is “”.
altFormat This option is used when an altField option is specified. it provides the format for the value to be written to the alternate element. By default its value is “”.
appendText This option is a string value to be placed after the element, intended to show instructions to the user. By default its value is “”.
autoSize This option when set to true resizes the element to accommodate the datepicker?s date format as set with the dateformat option. By default its value is false.
beforeShow This option is a callback function that?s invoked just before a datepicker is displayed, with the element and datepicker instance passed as parameters. This function can return an options hash used to modify the datepicker. By default its value is “”.
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. By default its value is null.
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 “”.
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.
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 “…”.
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.
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.
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. The option yearRange can be used to control which years are made available for selection. By default its value is false.
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”.
constrainInput This option if set true (the default), text entry into the element is constrained to characters allowed by the current dateformat option. By default its value is true.
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.
dateFormat This option specifies the date format to be used. By default its value is mm/dd/yy.
dayNames This option is a 7-element array providing the full day names with the 0th element representing Sunday. It can be used to localize the control. by default its value is [ “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday” ].
dayNamesMin This option is a 7-element array providing the minimal day names with the 0th element representing Sunday, used as column headers. It can be used to localize the control. by default its value is [ “Su”, “Mo”, “Tu”, “We”, “Th”, “Fr”, “Sa” ].
dayNamesShort This option specifies a 7-element array providing the short day names with the 0th element representing Sunday. It can be used to localize the control. By default its value is [ “Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat” ].
defaultDate This option sets the initial date for the control, overriding the default value of today, if the 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.
duration This option specifies the speed of the animation that makes the datepicker appear. It can be one of slow, normal, or fast, or the number of milliseconds for the animation to span. By default its value is normal.
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.
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.
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.
isRTL If this option is set to true, it specifies the localizations as a right-to-left language. By default its value is false.
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.
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.
monthnames This option is a 12-element array providing the full month names with the 0th element representing January. It can be used to localize the control. by default its value is [ “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” ].
monthNamesShort This option specifies a 12-element array providing the short month names with the 0th element representing January. It can be used to localize the control. by default its value is [ “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec” ].
navigationAsDateFormat If this option is 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.
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.
numberOfMonths This option specifies the number of months to show in the datepicker. By default its value is 1.
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.
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.
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.
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 PrevdefaultDatedayNamesMin.
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.
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.
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.
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.
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.
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.
showOn This option specifies when the datepicker should appear. The possible values are focus, button or both. by default its value is focus.
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 {}.
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.
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.
stepMonths This option specifies how many months to move when one of the month navigation controls is clicked. By default its value is 1.
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.
yearRange This option specifies limits on which years are displayed in the dropdown in the form: 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.
yearSuffix This option displays additional text after the year in the datepicker header. By default its value is “”.

jQuery UI datepicker()示例1

让我们以一个简单的示例来演示datepicker功能,该功能不会向datpicker()方法传递任何参数。



   
      
      jQuery UI Datepicker functionality
      
      
      

      
      
   
   
       
      

Enter Date:

jQuery UI datepicker()方法示例2

内联日期选择器:

让我们以一个简单的示例来演示内联日期选择器。



   
      
      jQuery UI Datepicker functionality
      
      
      
      
      
   
   
       
      Enter Date: 

jQuery UI datepicker()方法示例3

使用appendText,dateFormat,altField和altFormat:

让我们以一个示例来演示jQueryUI datepicker()方法中选项,appendText,dateFormat,altField和altFormat的用法。



   
      
      jQuery UI Datepicker functionality
      
      
      
      
      
   
   
       
      

Enter Date:

Alternate Date:

jQuery UI datepicker()方法示例4

使用showWeek,yearSuffix和showAnim:

让我们以一个示例来演示jQueryUI的datepicker函数中showWeek,yearSuffix和showAnim的用法。



   
      
      jQuery UI Datepicker functionality
      
      
      
      
      
   
   
       
      

Enter Date:

第二种方法

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

句法:

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

以下是此方法使用的操作的列表。

Action Description
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.
dialog( Date [, onSelect ] [, settings ] [, Pos ] ) This action displays datepicker in a jQuery ui dialog box .
getDate() This action returns the date corresponding to the selected date. This method does not accept any arguments.
hide() This action closes the previously opened date picker. This method does not accept any arguments.
isDisabled() This action checks if the date picker functionality is disabled. This method does not accept any arguments.
option(optionName) This action retrieves the value currently associated with the specified optionName.
option() This action gets an object containing key/value pairs representing the current datepicker options hash. This method does not accept any arguments.
option(optionName, Value) This action sets the value of the datepicker option associated with the specified optionName.
option(Options) This action sets one or more options for the datepicker.
refresh() This action redraws the date picker, after having made some external modifications. This method does not accept any arguments.
setdate(date) This action sets the specified date as the current date of the datepicker.
show() This action opens the date picker. If the datepicker is attached to an input, the input must be visible for the datepicker to be shown. This method does not accept any arguments.
widget() This action returns a jQuery object containing the datepicker.

jQuery UI datepicker()方法示例5

让我们以一个示例来演示上表中操作的用法。在此示例中,我们使用setDate()操作。



   
      
      jQuery UI Datepicker functionality
      
      
      
      
      
   
   
       
      

Enter Date: