How would I get to call the method showAlert() I have tried storing a reference to the plugin with this and $(this) inside the self variable but none seem to work. I'm calling the showAlert from inside the button click event.
(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开发者_如何学Python.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);
The way you have it written, showAlert
isn't a member of this
or self
. Just declare showAlert
before the methods
variable like this:
var showAlert = function() { alert("Please select an alternitive"); };
var methods = { /* etc etc */ };
Looks like you would just need to do this:
$('#myId').chooseAppointment('showAlert');
It looks like the plugin calls the method based on the method argument, and then applies other arguments based on the additional parameters passed. IE
$('#myId').chooseAppoinment('showAlert', 'Foo');
Would seem to call showAlert('Foo');
interally.
精彩评论