Created
February 1, 2009 10:48
-
-
Save higeorange/55847 to your computer and use it in GitHub Desktop.
jQuery AjaxPost
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.ajaxPost = function(callback, options) { | |
var default_options = { | |
data_type: 'html', | |
before_send: function() {}, | |
error_handler: function() {} | |
}; | |
options = $.extend(default_options, options) | |
return this.each(function(){ | |
if(this.tagName.toLowerCase() != 'form') return; | |
var f = $(this); | |
var submit = f.find('button[@type="submit"], input[@type="submit"]') | |
f.submit(function(e) { | |
e.preventDefault(); | |
$.ajax({ | |
url: f.attr('action'), | |
type: 'POST', | |
data: f.serialize() + '&mode=ajax', | |
dataType: options.data_type, | |
beforeSend: function() { | |
submit.attr('disabled', 'disabled') | |
options.before_send(); | |
}, | |
success: callback, | |
error: options.error_handler, | |
complete: function(xhr, textStatus) { | |
submit.removeAttr('disabled') | |
} | |
}); | |
}); | |
}); | |
} | |
})(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment