jQuery 手动触发日期选择器 onselect 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18979579/
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:54:10  来源:igfitidea点击:

Fire the datepicker onselect event manually

javascriptjqueryjquery-uidatepicker

提问by user2743057

Can anyone tell me how do you trigger the onselect event of datepicker? My code is working well (pulling some data from a database based on the value of datepickers altfield) but when the page loads it doesn't trigger the onselect event on the current date and it doesn't display anything; so I would like to do that manually.

谁能告诉我你如何触发datepicker的onselect事件?我的代码运行良好(根据 datepickers altfield 的值从数据库中提取一些数据)但是当页面加载时,它不会在当前日期触发 onselect 事件,也不会显示任何内容;所以我想手动完成。

<script>
     $( "#datepicker" ).datepicker
    (
    {
          altField: '#sheet',
          onSelect: function load() {
            $(document).ready(function() {
                    $.ajax({
                    type: 'POST',
                    url: 'test.php',
                    data: {sheetid: $('#sheet').val()},
                    success: function(data)
                    {
                        $("#content").html(data);
                    }
                });

        });
        },
          firstDay: 1});
    </script>

回答by Irvin Dominin

You can trigger a clickon the datepicker right after the set of the date.

您可以click在设置日期后立即触发日期选择器。

Code:

代码:

$(document).ready(function () {
    $("#datepicker").datepicker({
        altField: '#sheet',
        onSelect: function(date) {
            alert(date);
        },
        firstDay: 1
    });

    $('#datepicker').datepicker("setDate", new Date(2013,09,22) );
    $('.ui-datepicker-current-day').click(); // rapresent the current selected day

});

Demo: http://jsfiddle.net/IrvinDominin/DynuW/

演示:http: //jsfiddle.net/IrvinDominin/DynuW/

回答by scott.korin

I took this from the source code of the datepicker.js file. Note that thisshould be replaced with your input element (not the jquery object, the actual dom element). In my case, I was calling this from a keydown event.

我从 datepicker.js 文件的源代码中获取了这个。请注意,this应替换为您的输入元素(不是 jquery 对象,实际的 dom 元素)。就我而言,我是从 keydown 事件中调用它的。

// this is SUCH a hack.  We need to call onselect to call any
// specific validation on this input.  I stole this from the datepicker source code.
var inst = $.datepicker._getInst(this),
onSelect = $.datepicker._get(inst, "onSelect");
if (onSelect) {
    dateStr = $.datepicker._formatDate(inst);
    onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]);
}

回答by AdrianPop

Best solution

最佳解决方案

var inst = $.datepicker._getInst($("#date")[0]);
$.datepicker._get(inst, 'onSelect').apply(inst.input[0], [$("#date").datepicker('getDate'), inst]);

回答by scaryterry

This is the only solution I found that also works for a datetimepicker

这是我发现也适用于日期时间选择器的唯一解决方案

var $calendar =$('#from');
$.datepicker._getInst($calendar[0]).settings.onSelect()//or onClose or any event