jQuery UI datepicker - 尝试从点击日期捕获点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18640143/
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
jQuery UI datepicker - Trying to capture click event from date clicked
提问by James Pickering
We're using jquery UI 1.8.23 at my workplace.
我们在我的工作场所使用 jquery UI 1.8.23。
How can I capture the date clicked in datepicker, so I can format the current date into a datestamp and pass that value into a hidden form field?
如何捕获在 datepicker 中单击的日期,以便我可以将当前日期格式化为日期戳并将该值传递到隐藏的表单字段中?
Here's some code I've tried to get working:
这是我试图开始工作的一些代码:
$('.ui-state-default').live('click', function(){
console.log('date clicked');
var currentdate = $(this).text();
var d = currentdate;
var m = monthFormat($('.ui-datepicker-month').text());
var y = $('.ui-datepicker-year').text();
$('a.ui-state-default').removeClass('ui-state-highlight');
$(this).addClass('ui-state-highlight');
if (currentdate.length < 2) {
d = "0" + currentdate;
}
var datestamp = y + "-" + m + "-" + d;
console.log(datestamp);
$('#dateHidden').val(datestamp);
});
Thanks for looking at my code I appreciate any help you guys can offer.
感谢您查看我的代码,感谢你们提供的任何帮助。
回答by melancia
You don't need to do anything to format the date. The DatePicker
widget does it for you.
Just use the altField
and altFormat
properties:
您不需要做任何事情来格式化日期。该DatePicker
插件会为你。只需使用altField
和altFormat
属性:
$("#myDatePicker").datepicker({
// The hidden field to receive the date
altField: "#dateHidden",
// The format you want
altFormat: "yy-mm-dd",
// The format the user actually sees
dateFormat: "dd/mm/yy",
onSelect: function (date) {
// Your CSS changes, just in case you still need them
$('a.ui-state-default').removeClass('ui-state-highlight');
$(this).addClass('ui-state-highlight');
}
});
Documentation:
文档:
http://api.jqueryui.com/datepicker/#option-altField
http://api.jqueryui.com/datepicker/#option-altField
http://api.jqueryui.com/datepicker/#option-altFormat
http://api.jqueryui.com/datepicker/#option-altFormat
Demonstration:
示范:
回答by Praveen
jQuery Datepicker
gives the option of formatting date. So why don't you make use of that?
jQueryDatepicker
提供了格式化日期的选项。那你为什么不利用它呢?
$('element').datepicker({ dateFormat: 'dd-mm-yy' });
Also you don't need of a separate method to capture the click event. You the default method onSelect
此外,您不需要单独的方法来捕获单击事件。你默认的方法onSelect
$('input').datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function (date) {
//defined your own method here
alert(date);
}
})
Check this JSFiddle
检查这个JSFiddle
回答by PSN-RaffyBoy81
$('#calendar').datepicker({
inline: true,
firstDay: 1,
changeMonth: false,
changeYear: true,
showOtherMonths: true,
showMonthAfterYear: false,
yearRange: "2015:2025",
dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
dateFormat: "yy-mm-dd",
onSelect: function (date) {
alert(date);
}
});
回答by Suresh Atta
Your datepicker listens that.
你的日期选择器会听到那个。
Called 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.
在选择日期选择器时调用。该函数接收所选日期作为文本和日期选择器实例作为参数。这是指相关的输入字段。
$('.date-pick').datepicker( {
onSelect: function(date) {
//play with date here
},
----
----
---