【发布时间】:2011-12-31 11:46:14
【问题描述】:
如何从插件中获取变量的值或从插件中发送值?
例如,这是我的插件,它通过 ajax 检索用户信息并将用户信息存储在变量中,
(function($){
$.fn.extend({
get_authentication: function(options) {
var defaults = {
file: 'authentication_json.php',
location: 'cms/'
}
var options = $.extend(defaults, options);
var o = options;
var object = $(this); // now the object is the selected element such as #popup
$.get(http_root + rp_cms+ o.file, function(data){
if(data) var user_id = data.user_id;
// Redirect if the date returns as 'expired'.
if(data && data.error == 'expired') window.location = http_root + o.location;
//alert("Data Loaded: " + data);
},"json");
}
});
})(jQuery);
当我点击某个链接时,我会调用这个插件,
$('.get-user').click(function(){
$.fn.get_authentication();
alert(user_id);
});
我收到这样的错误。
有什么想法吗?
【问题讨论】:
-
在你的插件中不要使用对象作为变量名。 object 是一个 javascript 保留名称 quackit.com/javascript/javascript_reserved_words.cfm
标签: json jquery jquery-plugins