0

我正在使用 jquery 和 jquery ui 1.8.7。我在页面加载时创建了一个内联日期选择器,然后在我的 ajax 成功函数中,我调用 $('#mydiv').datepicker('refresh'); (我将在下面发布代码)。

如果数据已从 ajax 返回(例如在刷新时),则 beforeShowDay 调用 highlightDays() 函数。我知道在一切崩溃停止之前,我用正确的数据两次点击 highlightDays,我收到错误“TypeError:无法读取未定义的属性 0”。

似乎事件数组在一段时间后被破坏了,但我对 ajax 的了解还不够,无法真正说出发生了什么。谁能指出我正确的方向来解决这个问题?

        function highlightDays(date) {
                var day = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                console.log(typeof(events)); // This will return as an object twice before throwing an the above mentioned error
                if($.inArray(day, events) !== -1) {
                    return new Array(true, '');
                } else {
                    return new Array(false, '');
                }   
        }    
        function getEventData() {
                return $.ajax({
                    url: Drupal.settings.basePath + 'correct_path', 
                    data: search+'&path='+window.location.pathname,
                    dataType: 'json',
                    type: 'POST',
                    success: function(data, textStatus, jqXHR) {        
                        // save our returned data
                        events = new Object();
                        events = data;
                        $('#mydiv').datepicker("refresh");
                    }
                });
            }   

        function createDatepicker() {

            // Attach datepicker to the parent div so it returns as 
            // inline.
            $('#mydiv').datepicker({
                dateFormat: 'yy-mm-dd',
                speed: 'immediate',
                altField: '#edit-date-filter-value-datepicker-popup-1',
                beforeShowDay: function(date) {
                      if(typeof (_event_days) === 'undefined') {
                        return new Array(true, '');
                      } else {
                            highlightDays(date);
                        }
                    },
            });
            $('#myinput').hide();
        }   
getEventData();
createDatepicker();
4

1 回答 1

0

以下代码最终为我工作。两个很大的区别。重新设计 highlightDays 函数并将所有从 ajax 调用返回的数据添加到全局事件数组中,以便稍后在 highlightDays 中使用。

var events = [];
function highlightDays(date) {
        var dayClass = [true, ''];
          var date = $.datepicker.formatDate('yy-mm-dd', new Date(date));                     
                  $.each(events, function(key, value) { 
                    if (date === value[0])  {
                       dayClass = [true, "ui-state-highlight"];
                       return dayClass;

                    } else {
                        return dayClass;
                    }
                 });
              return dayClass;
          }


        function getEventData() {
            return $.ajax({
                url: 'myurl', 
                data: search+'&path='+window.location.pathname,
                dataType: 'json',
                type: 'POST',
                success: function(data, textStatus, jqXHR) {        
                    $.each(data, function (index, value) {
                       events.push([value]);
                    });
                          // popup is the date input field
                          // attaching datepicker to the parent div makes 
                          // it an inline picker    
                          popup.parent().datepicker('refresh');
                }
            });
        }


        function createDatepicker() {

           popup.parent().datepicker({
                dateFormat: 'yy-mm-dd',
                 beforeShowDay: highlightDays

            });
            popup.hide();
        }




        function getSearchString() {
            // Return a search string to pass to the ajax call
            var search = window.location.search;
            if(search !== '') {
                while(search.charAt(0) === '?') {
                    // remove the leading question mark in the search string
                    search = search.substr(1);
                }
            }
            return search;
        }

        // If our popup selector exists, create the datepicker and get event data
        if (popup.length) {     
            var search = getSearchString();
            getEventData();
            createDatepicker();
        }
于 2012-11-09T18:49:49.090 回答