jQuery UI datepicker - 尝试从点击的日期捕获点击事件 [英] jQuery UI datepicker - Trying to capture click event from date clicked

查看:115
本文介绍了jQuery UI datepicker - 尝试从点击的日期捕获点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在工作场所使用jquery UI 1.8.23。



如何捕获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



演示:



http://jsfiddle.net/sFBn2/10/


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:

http://jsfiddle.net/sFBn2/10/

这篇关于jQuery UI datepicker - 尝试从点击的日期捕获点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆