[英]JQuery function present but saying method undefined
I am relatively new to Jquery and so accept that the answer could be pretty obvious. 我对Jquery比较陌生,因此接受答案可能非常明显。 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
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.
当然,所需的变量var是在检索正确的数据,并且我已确保插件在标头中正确链接。 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]);
});
});
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 is undefined in that context and scope. 在该上下文和范围内,cal是未定义的。
I have a feeling that 我有种感觉
cal = window.plugins.calendarPlugin;
is not assigned correctly. 未正确分配。
... thanks for the link (see comments) -- @Ohgodwhy. ...感谢您的链接(请参阅评论)-@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){
Whenever you call the function just use cal.createEvent (you'll need to edit the click event handler as such. 每当调用该函数时,只需使用cal.createEvent(就需要编辑click事件处理程序。
This is going to sound stupid but... are you sure the plugin JS file was included and executed before this script? 这听起来很愚蠢,但是...您确定在此脚本之前包含并执行了插件JS文件吗? 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)!
如果在此脚本执行后包含插件(日历插件javascript文件),则calendarPlugin对象将不存在(因此,它将保持未定义状态)!
It is a scope issue which is preventing the var cal
from being hoisted to the top. 这是一个范围问题,导致无法将
var cal
提升到顶部。 Move your function declaration within the ready function. 将函数声明移至ready函数中。 You're also getting undefined because you're calling
createEvent
as a method of cal
but you haven't created a class object. 您还会得到未定义的信息,因为您正在调用
createEvent
作为cal
的方法,但尚未创建类对象。
$(document).ready(function() {
var cal;
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]);
});
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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.