只有一个关于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返回的日期没有加载,我得到一个错误,说'no method: push‘。
我这样做是完全正确的,还是有更好的方法?因为我假设我不能只在页面上使用php,因为js是在php之前加载的,对吗?
任何帮助都将不胜感激。
发布于 2013-03-26 20:03:20
您使用{}将dates声明为对象,而不是使用[]声明为数组。不能在对象上调用.push()。
发布于 2013-03-26 20:02:33
dates是一个对象,而对象没有名为push的函数。数组确实有这些函数。
无论如何,只需这样做:
var dates = ['2013/4/4':'some description' , '2012/6/6':'some other description'];
发布于 2013-03-26 20:06:23
Date不是一个数组,所以你不能推入它。您需要将日期定义为数组
var dates = [{'2013/4/4':'some description'} , {'2012/6/6':'some other description'} ]上面的代码创建了一个对象数组。
https://stackoverflow.com/questions/15636555
复制相似问题