jQuery UI datepicker - 尝试从点击的日期捕获点击事件 [英] jQuery UI datepicker - Trying to capture click event from date clicked
问题描述
如何捕获datepicker中点击的日期,因此我可以将当前日期格式化为datestamp并将该值传递给一个隐藏的表单域?
这是我尝试工作的一些代码:
$('。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' );
});
感谢您查看我的代码,感谢您可以提供的任何帮助。
您不需要做任何事情来格式化日期。 DatePicker
小部件为您做。
只需使用 altField
和 altFormat
属性:
$(#myDatePicker)。datepicker({
//接收日期的隐藏字段
altField:#dateHidden,
//您想要的格式
altFormat:yy-mm-dd,
//用户实际看到的格式
dateFormat:dd / mm / yy,
onSelect:function(date){
//您的CSS更改,以防您仍然需要
$('a.ui-state-default')。removeClass('ui-state-highlight ');
$(this).addClass('ui-state-highlight');
}
});
文档:
http://api.jqueryui.com/datepicker/#option-altField
http://api.jqueryui.com/datepicker/# option-altFormat
演示:
We're using jquery UI 1.8.23 at my workplace.
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?
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.
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:
$("#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-altFormat
Demonstration:
这篇关于jQuery UI datepicker - 尝试从点击的日期捕获点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!