我如何调用方法showAlert()我已经尝试在自变量中存储对插件的引用和$(this),但似乎没有工作。我在按钮点击事件中调用showAlert。
(function($){
//Add your default settings values here
var settings = {
//Replace with your own settings
testValue:"FOO"
}
var selectedArr=[];
var self=this;
var methods = {
init : function(options){
return this.each(function(){
//if options exists, let's merge them with our default settings
if(options){
$.extend(settings,options);
}
var $this=$(this);
var button = $this.find('.schedule .time a');
button.click(function(evt){
evt.preventDefault();
selectedArr.push( $(this).addClass('selected') );
$(this).parent().find('input').attr('checked','checked');
self.showAlert();
})
});
}
}
showAlert: function(){
alert("Please select an alternitive");
}
//Setting namespace. Under no circumstance should a single plugin ever claim more than one namespace in the jQuery.fn object.
//See http://docs.jquery.com/Plugins/Authoring for an explaination of the method used here
$.fn.chooseAppointment = function(method){
if(methods[method]){
return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
}else if(typeof method === 'object' || !method){
return methods.init.apply(this,arguments);
}else{
$.error('Method ' + method + ' does not exist on jQuery.chooseAppointment');
}
};
})(jQuery);
答案 0 :(得分:0)
看起来你只需要这样做:
$('#myId').chooseAppointment('showAlert');
看起来插件基于方法参数调用方法,然后根据传递的其他参数应用其他参数。 IE
$('#myId').chooseAppoinment('showAlert', 'Foo');
似乎会在某种程度上致电showAlert('Foo');
。
答案 1 :(得分:0)
您编写的方式,showAlert
不是this
或self
的成员。只需在showAlert
变量之前声明methods
,如下所示:
var showAlert = function() { alert("Please select an alternitive"); };
var methods = { /* etc etc */ };