-
-
Save MhdSyrwan/5cfebf927816f9c75619100b4f632228 to your computer and use it in GitHub Desktop.
A simple jQuery plugin to ajaxify a form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$(document).ready(function() { | |
$('#myform').ajaxify({ | |
afterSend: function() { | |
# show progress gif | |
}, | |
complete: function() { | |
# hide progress gif | |
# close or hide the form | |
} | |
}); | |
}); | |
})(jQuery) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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