繁体   English   中英

从变量获取事件

[英]getting the event from variable

我从github学习jquery日历: https//github.com/themouette/jquery-week-calendar

我使用C#背后的代码从数据库中提取数据,并将其存储在hiddenfield中,然后javascript以字符串形式读取了hiddenfield字段值。

我将跳过如何从数据库中获取值,在这个问题中,我将事件硬编码为sampleEvents var。

工作的Javascript:

 var eventData = {
      events: [
{'id':1, 'start': new Date(2015, 3, 27, 12), 'end': new Date(2015, 3, 27, 13, 35),'title':'Lunch with Mike'},
{'id':2, 'start': new Date(2015, 3, 28, 10), 'end': new Date(2015, 3, 28, 14, 45),'title':'Dev Meeting'}
]};

无法使用Javascript:

var sampleEvents = "{'id':1, 'start': new Date(2015, 3, 27, 12), 'end': new Date(2015, 3, 27, 13, 35),'title':'Lunch with Mike'},{'id':2, 'start': new Date(2015, 3, 28, 10), 'end': new Date(2015, 3, 28, 14, 45),'title':'Dev Meeting'}";

 var eventData = {
      events: [
         sampleEvents
]};

错误信息:

Uncaught TypeError: Cannot read property 'getTime' of undefined

无法使用Javascript 2:

var sampleEvents = "[{'id': 1,'start': new Date(2015, 3, 27, 12),'end': new Date(2015, 3, 27, 13, 35),'title': 'Lunch with Mike'},{'id': 2,'start': new Date(2015, 3, 28, 10),'end': new Date(2015, 3, 28, 14, 45),'title': 'Dev Meeting'}]";

var array = JSON.parse(sampleEvents);

var eventData = {
       events: 
           sampleEvents
};

错误消息:

Uncaught SyntaxError: Unexpected token '

谁能告诉我,我错过了什么?

因为您创建的sampleEvents是字符串而不是数组。

event接受array而不是string

要使用数组,请尝试以下操作:

var sampleEvents = [
    {
        'id':1, 
        'start': new Date(2015, 3, 27, 12), 
        'end': new Date(2015, 3, 27, 13, 35),
        'title':'Lunch with Mike'
    },
    {
        'id':2, 
        'start': new Date(2015, 3, 28, 10), 
        'end': new Date(2015, 3, 28, 14, 45),
        'title':'Dev Meeting'
    }
];

要使用字符串,请尝试以下操作:

var sampleEvents = '['+
    '{"id":1, "start": "'+new Date(2015, 3, 27, 12)+'", "end": "'+new Date(2015, 3, 27, 13, 35)+'","title":"Lunch with Mike"},'+
    '{"id":2, "start": "'+new Date(2015, 3, 28, 10)+'", "end": "'+new Date(2015, 3, 28, 14, 45)+'","title":"Dev Meeting"}'+
    ']';

var sampleEventsArray = JSON.parse(sampleEvents);

暂无
暂无

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

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