Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $('form').submit(function() {
- var valuesToSubmit = $(this).serialize();
- $.ajax({
- type: "POST",
- url: $(this).attr('action'), //sumbits it to the given url of the form
- data: valuesToSubmit,
- dataType: "JSON" // you want a difference between normal and ajax-calls, and json is standard
- }).success(function(json){
- //act on result.
- alert('Hello world');
- });
- return false; // prevents normal behaviour
- alert('False');
- });
- $('#form').on('ajax:success', function(event, data, status, xhr) {
- alert('hello world');
- });
- $(document).ready(function(){
- $(document).bind('ajaxError', 'form', function(event, jqxhr, settings, exception){
- // note: jqxhr.responseJSON undefined, parsing responseText instead
- $(event.data).render_form_errors( $.parseJSON(jqxhr.responseText) );
- });
- });
- (function($) {
- $.fn.modal_success = function(){
- // close modal
- this.modal('hide');
- alert('hello world');
- // clear form input elements
- // todo/note: handle textarea, select, etc
- this.find('form input[type="text"]').val('');
- // clear error state
- this.clear_previous_errors();
- };
- $.fn.render_form_errors = function(errors){
- $form = this;
- this.clear_previous_errors();
- model = this.data('model');
- // show error messages in input form-group help-block
- $.each(errors, function(field, messages){
- $input = $('input[name="' + model + '[' + field + ']"]');
- $input.closest('.form-group').addClass('has-error').find('.help-block').html( messages.join(' & ') );
- });
- };
- $.fn.clear_previous_errors = function(){
- $('.form-group.has-error', this).each(function(){
- $('.help-block', $(this)).html('');
- $(this).removeClass('has-error');
- });
- }
- }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement