jQuery DatePicker:获取选定的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2311608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:13:15  来源:igfitidea点击:

jQuery DatePicker: Get Selected Date

jqueryjquery-uidatepickerjquery-ui-datepicker

提问by Matt

I need to be able to grab the selected date from the Datepicker and populate another textbox with the selected value. I am currently using the onClosefunction like this:

我需要能够从 Datepicker 中获取所选日期并使用所选值填充另一个文本框。我目前正在使用这样的onClose功能:

$('#defaultDate').datepicker({
    onClose:function(theDate) {
        $('#txtEntry1').text = theDate;
    }
});

回答by Matt Ball

There's an easier way to do it. Use the built-in alt-field optionand you're done.

有一种更简单的方法来做到这一点。使用内置的alt-field 选项就大功告成了。

Populate an alternate field with its own date format whenever a date is selected using the altFieldand altFormatoptions. This feature could be used to present a human-friendly date for user selection, while passing a more computer-friendly date through for further processing.

每当使用altFieldaltFormat选项选择日期时,用其自己的日期格式填充备用字段。此功能可用于为用户选择提供人性化的日期,同时传递更适合计算机的日期以供进一步处理。

回答by Thomas Ahle

Have you looked through the docs? http://docs.jquery.com/UI/Datepicker#events

你看过文档吗?http://docs.jquery.com/UI/Datepicker#events

onSelect function(dateText, inst)

onSelect 函数(dateText,inst)

Allows you to define your own event when the datepicker is selected. The function receives the selected date as text and the datepicker instance as parameters. this refers to the associated input field. Code examples

允许您在选择日期选择器时定义自己的事件。该函数接收所选日期作为文本和日期选择器实例作为参数。这是指相关的输入字段。代码示例

Supply a callback function to handle the onSelect event as an init option.

提供一个回调函数来处理 onSelect 事件作为 init 选项。

$('.selector').datepicker({
   onSelect: function(dateText, inst) { ... }
});