Skip to content

Instantly share code, notes, and snippets.

@rantastic
Created March 21, 2013 17:46
Show Gist options
  • Save rantastic/5215045 to your computer and use it in GitHub Desktop.
Save rantastic/5215045 to your computer and use it in GitHub Desktop.
JS: Form submit
$('form').on('click','[trigger="formSubmit"]',function(){
var button = $(this);
var form = button.closest('form');
button.html(' ');
button.spin('small');
button.attr('disabled','disabled');
form.find('input,select,textarea').closest('.control-group').removeClass('error');
form.find('input,select,textarea').next().empty();
var postData = {};
form.find('input,select,textarea').each(function(index){
if($(this).attr('type') == 'checkbox' || $(this).attr('type') == 'radio'){
postData[$(this).attr('id')] = $(this).is(':checked');
}
else{
postData[$(this).attr('id')] = $(this).val();
}
});
$.post('ajax/'+form.attr('form-action'), postData,
function(data){
if(!("error" in data)){
button.spin(false);
button.html('Submit');
button.removeAttr('disabled');
}
else{
$.each(data.error,function(i,v){
form.find('#'+v[0]).closest('.control-group').addClass('error');
form.find('#'+v[0]).next().html(v[1]);
});
button.spin(false);
button.html('Submit');
button.removeAttr('disabled');
}
},
'json');
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment