| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 
 | (function($){
	// Methodes
	var methods = {
		'init' : function(){
 
			// Lorsque le formulaire est valide
			this.bind('submit', function(event){
 
				// On empeche l'envoi
				event.preventDefault();
 
				$(this).sendForm('loading', "transaction AJAX en cours");
 
				// debug
				//console.log( this, "submit", $(this).serialize() );
 
				// On sauvegarde la valeur de this
				var self = this;
 
				// On exécute une requete AJAX
				$.post("une url", $(self).serialize(), function(data){
					var current = null;
 
					// debug
					//console.log( this, self);
 
					for(idx in data){
						current = data[idx];
 
						$(self).find('[name*="' + current.key + '"]').after('<span class="erreur_form">' + current.message + '</span>');
					}
 
					$(self).sendForm('loading', "transaction AJAX terminée");
				}, "json");
			});
 
			return this;
		},
		'loading' : function(msg){
			alert(msg);
 
			return this;
		}
	};
 
	// Point de lancement
	$.fn.sendForm = 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');
		}
	};
})(jQuery); | 
Partager