jQuery函数存在,但说方法未定义 [英] JQuery function present but saying method undefined

查看:768
本文介绍了jQuery函数存在,但说方法未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Jquery比较陌生,因此接受答案可能非常明显.我有一个提供功能的插件,但是运行程序时出现控制台错误:

I am relatively new to Jquery and so accept that the answer could be pretty obvious. I have a plugin that provides a function, but when I run my program I get the console error:

Cannot call method 'createEvent' of undefined 

就像我说的那样,我可能已经错过了一些简单的事情,但是我找不到.当然,所需的变量var是在检索正确的数据,并且我已确保插件在标头中正确链接.如果有人得到任何建议,他们可以提供,将不胜感激!谢谢.

Like I said I have probably missed something simple but I cannot find what. The var desiredValue is, of course, retrieving the correct data and I have ensured the plugin is linked correctly in the header. If anyones got any advice they can give that would be very much appreciated! Thanks.

function createEvent(title,location,notes, startDate, endDate){
  var title= "My Appt";
  var location = "Los Felix";
  var notes = "me testing";
  var startDate = "2012-11-23 09:30:00";
  var endDate = "2012-11-23 12:30:00";

  cal.createEvent(title,location,notes,startDate,endDate);
}

var cal;

$(document).ready(function() {
  cal = window.plugins.calendarPlugin;


  $('.calinfo').live('click', function() {    
    var desiredValue = $(this).parent().prev().find('.calendar').val();                                                
    var calInfo = desiredValue.split(',');

    createEvent(calInfo[0], calInfo[1], calInfo[2], calInfo[3], calInfo[4]);
  });                              
});    

推荐答案

在cal分配后放置一个调试点.问题是您的函数中的以下代码行:

Place a debug point after the cal assignment. The problem is this line of code within your function:

cal.createEvent(title,location,notes,startDate,endDate);

cal在该上下文和范围内未定义.

cal is undefined in that context and scope.

我有种感觉

cal = window.plugins.calendarPlugin;

分配不正确.

...感谢您的链接(请参阅评论)-@Ohgodwhy.

... thanks for the link (see comments) -- @Ohgodwhy.

说实话,该插件的编写效果不佳.但是,您应该做的只是编辑函数的第一行:

That plugin isn't written very well, honestly. But, what you should do is just edit the first line of your function:

window.plugins.calendarPlugin.prototype.createEvent = function(title,location,notes, startDate, endDate){

每次调用该函数时,只需使用cal.createEvent(您需要像这样编辑click事件处理程序.

Whenever you call the function just use cal.createEvent (you'll need to edit the click event handler as such.

这听起来很愚蠢,但是...您确定在此脚本之前包含并执行了插件JS文件吗?如果您在执行此脚本后添加了插件(日历插件javascript文件),则calendarPlugin对象将不存在(因此,它将保持未定义状态!)!

This is going to sound stupid but... are you sure the plugin JS file was included and executed before this script? If you're including the plugin (calendar plugin javascript file) after this script executes, the calendarPlugin object won't exist (hence, it will remain undefined)!

这篇关于jQuery函数存在,但说方法未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆