0

只是一个关于 datepicker 和 json 的快速问题。我有这个代码:

var dates = {'2013/4/4':'some description' , '2012/6/6':'some other description'}; 

                    function date_propogate() {
                    console.log('in function');
                        $.getJSON('URL OF JSON', function(data) {
                        date_items = data.items;
                        console.log(date_items);
                            $.each(date_items, function(index, date_item) {
                                dates.push(date_item.sdate);
                            });
                        });
                    }

                    $(document).ready(function() {

                    date_propogate();

                    $('#datepicker').datepicker({                
                        beforeShowDay: function(date) {

                        var search = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + (date.getDate());


                        if (dates[search]) {
                                return [true, 'highlight', dates[search] || ''];
                                }

                                return [false, '', ''];
                            }

                        });
                    });

JSON 页面返回以下内容:

{"items":[{"sdate":"2013-02-25","edate":"2013-02-27","cost":"200","id":"1"}]}

我有两个显示正常的静态日期,但返回的日期:date_items.sdate 未加载,我收到一条错误消息,提示“无方法:推送”。

我这样做是正确的还是有更好的方法?正如我假设我不能只在页面上使用 php,因为 js 是在 php 之前加载的,对吗?

任何帮助将不胜感激。

4

3 回答 3

2

您已dates使用 声明为对象{},而不是使用[]. 你不能调用.push()一个对象。

于 2013-03-26T12:03:20.207 回答
0

日期是一个对象,而一个对象没有一个叫做 push 的函数。数组确实有这些功能。

无论如何,只需这样做:

var dates = ['2013/4/4':'some description' , '2012/6/6':'some other description'];

于 2013-03-26T12:02:33.077 回答
0

Dates 不是一个数组,所以你不能推送到它。您需要将日期定义为数组

var dates = [{'2013/4/4':'some description'} , {'2012/6/6':'some other description'} ]

上面创建了一个对象数组。

于 2013-03-26T12:06:23.107 回答