简体   繁体   English

jQuery ui datepicker事件触发得太早

[英]jQuery ui datepicker event firing too early

I have a datepicker on my site and I want to format certain dates by adding a class to every <a> where the text equals some value. 我的网站上有一个日期选择器,我想通过在文本等于某个值的每个<a>中添加一个类来设置某些日期的格式。 The code below works if I run updateDatepicker(); 如果我运行updateDatepicker();下面的代码将起作用updateDatepicker(); from the console when the datepicker is visible or if I add half a second delay in the function but it doesn't work when the onChangeMonthYear event fires and runs the function. 当可见日期选择器时,或者如果我在该函数中添加了半秒的延迟,则从控制台访问它,但是当onChangeMonthYear事件触发并运行该函数时,它将不起作用。 I know the function is running because I had console.log("updateDatepicker") inside to test it. 我知道该函数正在运行,因为我内部有console.log("updateDatepicker")进行了测试。

Here's my code: 这是我的代码:

function showDatePicker(){
    $( "#datepicker" ).datepicker({
        onChangeMonthYear: function(year, month, inst) { updateDatepicker(); }
    });

function updateDatepicker(){
    var days = $(".ui-datepicker-calendar a.ui-state-default").not(".ui-priority-secondary");

    for (var i=0; i < days.length; i++) {
        if(days[i].text == "2"){
            $(days[i]).addClass("datepickerActivity");
        }
    }
}

How do I make this work without relying on a delay in the updateDatepicker() function? 我如何在不依赖updateDatepicker()函数延迟的情况下进行这项工作?

Make sure the jQuery onLoad mechanism is used for either your call to showDatePicker, or the code within that function. 确保对调用showDatePicker或该函数中的代码使用jQuery onLoad机制。 This will ensure that everything is loaded and ready for use within your other functions when those events fire. 这将确保在这些事件触发时,所有内容都已加载并可以在其他函数中使用。

$(function () {
    showDatePicker();
});

or 要么

function showDatePicker(){
    $(function () {
        $( "#datepicker" ).datepicker({
            onChangeMonthYear: function(year, month, inst) { updateDatepicker(); }
        });
    }
}

Edit : Since element loading isn't the issue, it appears that the onChangeMonthYear callback gets called before the calendar renders. 编辑 :由于元素加载不是问题,因此似乎呈现日历之前调用了onChangeMonthYear回调。 Therefore, you will have to wait for the current call stack to clear out. 因此,您将不得不等待当前的调用堆栈清除。 You can accomplish this with a zero-wait timeout inside your updateDatepicker function. 您可以在updateDatepicker函数内部使用零等待超时来完成此操作。

function updateDatepicker() {
    setTimeout(function () {
        var days = $(".ui-datepicker-calendar a.ui-state-default").not(".ui-priority-secondary");

        for (var i=0; i < days.length; i++) {
            if(days[i].text == "2"){
                $(days[i]).addClass("datepickerActivity");
            }
        }
    }, 0);
}

Note, this timeout is for 0 milliseconds, but it should still get put on the call stack after the calendar render call. 请注意,此超时时间为0毫秒,但在日历渲染调用之后仍应将其放入调用堆栈中。 This is more of a limitation of the available callbacks that jQuery UI offers for the date picker. 这更多地是jQuery UI为日期选择器提供的可用回调的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM