Skip to content

Instantly share code, notes, and snippets.

@mdellavo
Created March 8, 2012 14:08
Show Gist options
  • Save mdellavo/2001140 to your computer and use it in GitHub Desktop.
Save mdellavo/2001140 to your computer and use it in GitHub Desktop.
A simple jQuery plugin to ajaxify a form
(function($) {
$.fn.ajaxify = function(settings) {
this.each(function() {
$(this).submit(function() {
var context = $(this);
var _settings = {
url: this.action,
type: this.method,
data: context.serialize(),
context: context
};
if (settings)
$.extend(_settings, settings);
_settings.beforeSend = function() {
context.find(':input').attr('disabled', 'disabled');
if (settings.beforeSend)
settings.beforeSend.apply(this,
[context].concat(arguments));
}
_settings.complete = function() {
context.find(':input').attr('disabled', null);
if (settings.complete)
settings.complete.apply(this,
[context].concat(arguments));
}
$.ajax(_settings);
return false;
});
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment