How to use events is jquery plugin?
Sorry this is my first time trying to create a plugin, so I might be way off. I have read the docs but got confused, and I decided to learn by trying....
I am trying to make a simple form validation plugin
(function($){
$.fn.bluevalidate = function(options){
var defaults = {
errorMsg : 'You have an error',
required : ''
};
var opt = $.extend(defaults,options);
return this.each(function(index,element){
var e = $(element);
if(opt.required!='')
{
e.bind('blur',function(){
alert("required is here");
});
}
else
{
alert("NOOOOOOOOO");
}
});
};
})( jQuery )
And I am calling it like
$('#username').bluevali开发者_开发技巧date({
required:'This field is required';
})
I want to get the alert when the user clicks off the field, but as you can guess, my method isn't working, please tell me what I am doing wrong....
There's an extra ;
in there, it should be:
$('#username').bluevalidate({
required:'This field is required'
})
When declaring an object literal, your properties should not be followed by a ;
(you should however have a ,
between properties). This is the main issue:
required:'This field is required'; //the ; shouldn't be here
You can test it out here.
精彩评论