【问题标题】:jQuery UI datepicker - TypeError: O is undefinedjQuery UI datepicker - TypeError: O is undefined
【发布时间】:2014-08-05 12:47:33
【问题描述】:

我正在制作 CakePHP 应用程序,目前我正在编写脚本来发出 ajax 请求以突出显示 jQuery datePicker 的保留日期,并且 onSelect 使用 jQuery Accordion 发出另一个 Ajax 请求以显示有关特定日期的预订信息。

但是,我的日历没有显示,并且在 Firebug 控制台中出现错误 - “类型错误:O 未定义。函数 i(t, i){ 在 jquery-ui.custom.min.js 文件中”。在实施某些日子的亮点之前,脚本运行良好。

查看(日历.ctp):

<?php 
    $this->set('title_for_layout', __('Calendar'));
    $this->Html->css('jquery-ui.custom', null, array('inline' => false));
    $this->Html->script('jquery-ui.custom.min', array('inline' => false));  
?>
<div class="row">
    <div class="span3" id="datepicker"></div>
    <div class="span7" id="hours"></div>
</div>

我的脚本:

$(document).ready(function() {
    $.ajax({
        url: '<?php echo Router::url(array('controller' => 'streservations', 'action' => 'highlight'), true);?>',
        type: "GET",
        cache: false,           
        data: {
            action : 'showdates'
        },
        dataType: 'json',
        success: function(reservations){   
            //alert(reservations[0][0]);
            console.log(reservations);
            $( "#datepicker" ).datepicker({ //initialize datepicker
                defaultDate: '<?php echo $this->Time->format('Y-m-d', time()) ?>',
                maxDate: '<?php echo $this->Time->format('Y-m-d', '+1 month') ?>',
                dateFormat: 'yy-mm-dd',
                beforeShowDay: function(date){                        
                    var y = date.getFullYear().toString(); // get full year
                    var m = (date.getMonth() + 1).toString(); // get month.
                    var d = date.getDate().toString(); // get Day
                    if(m.length == 1){ m = '0' + m; } // append zero(0) if single digit
                    if(d.length == 1){ d = '0' + d; } // append zero(0) if single digit                        
                    var currDate = y+'-'+m+'-'+d;
                    console.log(currDate);
                    /*
                    $.each(reservations, function(k, v){
                        //alert(v.date + ' ' + v.count);
                        if(v.date == currDate && v.count > 3){
                            return [true, "ui-highlight"];
                        }
                        else{
                            return [true];
                        }
                    });
                    /*for(var i = 0; i < reservations.lenght; i++){
                        alert(reservations[i][1]);
                        if(*reservations[i][1] == currDate*reservations.indexOf(currDate) >= 0){
                            return [true, "ui-highlight"];  
                        }else{
                            return [true];
                        }
                    }*/                     
                },
                onSelect : function(dateText,inst) //when selecting date, make AJAX call to show accordion
                {
                    $.ajax({
                        url: '<?php echo Router::url(array('controller' => 'streservations', 'action' => 'hours'), true);?>',
                        type: "GET",
                        cache: false,
                        dataType: 'json',
                        data: {
                            action: 'accord',
                            date: dateText
                        },
                        success: function(data){
                            $("#hours").accordion({collapsible: true, clearStyle: true, heightStyle: "content"});
                            $('#hours').empty();
                            $.each(data, function(k, v){
                                $('#hours').append('<li><h3><div>' + v.title + ' ' + v.start + '</div></h3><div>' + 'Operation: ' + v.name + '</br>' + 'Name: ' + v.title + '</br>'+ 'Time: ' + v.start + '</br>'+'</div></li>');
                            });
                            $("#hours").accordion( "refresh" );
                        }
                    });
                }
            });
        } 
    });
});

更新: 因此,我将脚本版本更改为开发版本(jquery-ui.custom),现在我收到此错误:

TypeError: daySettings 未定义

unselectable = (otherMonth && !selectOtherMonths) || !daySettings[0] ||

【问题讨论】:

  • 脚本的缩小版本用于生产,而不是开发。使用完整的脚本以获得更好的错误信息。

标签: javascript jquery ajax cakephp datepicker


【解决方案1】:

我想通了:我需要在 beforeShowDay 函数中返回数组:

               beforeShowDay: function(date){                        
                    var y = date.getFullYear().toString(); // get full year
                    var m = (date.getMonth() + 1).toString(); // get month.
                    var d = date.getDate().toString(); // get Day
                    if(m.length == 1){ m = '0' + m; } // append zero(0) if single digit
                    if(d.length == 1){ d = '0' + d; } // append zero(0) if single digit                        
                    var currDate = y+'-'+m+'-'+d;
                    console.log(currDate);                        
                    //return [true, "ui-highlight", ''];                         

                    for(var i = 1; i < reservations.lenght; i++){
                        console.log(reservations[i].date);
                        if(reservations[i].date == currDate){
                            return [true, 'ui-highlight', ''];  
                        }/*else{
                            return [true];
                        }*/

                    }
                    return [true, '', ''];
                },

但是我的每一天都没有被突出显示,这段代码没有被执行:

if(reservations[i].date == currDate){
                            return [true, 'ui-highlight', ''];  
                        }

我的预订 JSON 数组是:[Object { date="2014-07-18", count="3"}, Object { date="2014-08-05", count="1"}, Object { date="2014-08-06", count="2"}]

【讨论】:

    【解决方案2】:

    也许是因为你拼错了“长度”:

     for(var i = 1; i < reservations.lenght; i++){
    

    【讨论】:

      猜你喜欢
      • 2013-04-25
      • 1970-01-01
      • 2014-09-09
      • 2019-09-27
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多