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

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

jQuery UI datepicker - Trying to capture click event from date clicked

jqueryjquery-uidatepicker

提问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 DatePickerwidget does it for you. Just use the altFieldand altFormatproperties:

您不需要做任何事情来格式化日期。该DatePicker插件会为你。只需使用altFieldaltFormat属性:

$("#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:

示范:

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

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

回答by Praveen

jQuery Datepickergives 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.

你的日期选择器会听到那个。

see onSelect() function

onSelect() 函数

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
    },
----
----
---