Created
May 8, 2012 10:31
-
-
Save Edwin-Luijten/2634115 to your computer and use it in GitHub Desktop.
JS/JQUERY : A simple function to create an ajax 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
$.fn.ajaxForm = function(opts) { | |
var defaults = { | |
//Default values for options | |
ajaxType : 'post', | |
ajaxCache : false | |
} | |
// extend the options from defaults with user's options | |
var options = $.extend(defaults, opts || {}); | |
return this.each(function(){ // jQuery chainability | |
$(this).submit(function(e){ | |
e.preventDefault(); | |
var form = this, url = $(this).attr("action"); | |
$.ajax({ | |
type: options.ajaxType, | |
url: url, | |
cache: options.ajaxCache, | |
data: $(this).serialize() | |
}).done(function(data){ | |
$(form)[0].reset(); // Reset form after it is send | |
$(form).find(options.responseContainer).html(data); | |
}); | |
return false; | |
}); | |
}); | |
} |
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
$('#your-form-id').ajaxForm({ | |
responseContainer : '.your-form-response-container' //May be a class(.) or id(#) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment