Skip to content

Instantly share code, notes, and snippets.

@MhdSyrwan
Forked from mdellavo/jquery.ajaxify.js
Last active July 18, 2018 14:16
Show Gist options
  • Save MhdSyrwan/5cfebf927816f9c75619100b4f632228 to your computer and use it in GitHub Desktop.
Save MhdSyrwan/5cfebf927816f9c75619100b4f632228 to your computer and use it in GitHub Desktop.
A simple jQuery plugin to ajaxify a form
(function($) {
$(document).ready(function() {
$('#myform').ajaxify({
afterSend: function() {
# show progress gif
},
complete: function() {
# hide progress gif
# close or hide the form
}
});
});
})(jQuery)
(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);
settings.afterSend.apply(this, [context].concat(arguments));
return false;
});
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment