Skip to content

Instantly share code, notes, and snippets.

@RimonEkjon
Forked from mollwe/jquery.ajaxSubmit.js
Created August 26, 2014 04:25
Show Gist options
  • Save RimonEkjon/956432fb62f3d1540e3c to your computer and use it in GitHub Desktop.
Save RimonEkjon/956432fb62f3d1540e3c to your computer and use it in GitHub Desktop.
Gets all input values within form and submits form with ajax.
/*!
* jquery.ajaxSubmit.js - v 0.9.3 (2011-11-29)
* Copyright (C) 2011 by Adam Ydenius ([email protected]) | http://mollwe.se
* Dual licensed under MIT and GPL.
*//*
* Gets all input (with name attribute) values within form and submits form with ajax.
* First argument, options, is object with:
* Properties:
* - url: override form action.
* - method: override form method.
* - data: extend data found from inputs.
* Callbacks ("this" variable is jQuery(form element)):
* - beforeSend: function(jqXHR, ajaxSettings) (cancellable: return false)
* - success: function(data, textStatus, jqXHR)
* - error: function(jqXHR, textStatus, errorThrown)
* - complete: function(jqXHR, textStatus)
*
* After initialization you can call $(elem).ajaxSubmit("method") where available
* methods is:
* - submit: trigger submit manually
* - option: get or set settings
* - destroy: unbind events and remove data for ajaxSubmit
*
* Events ("this" variable is form element):
* - ajaxsubmitbeforeSend: function(event, jqXHR, ajaxSettings, cancelled) (cancellable: return false or event.preventDefault())
* - ajaxsubmitsuccess: function(event, data, textStatus, jqXHR)
* - ajaxsubmiterror: function(event, jqXHR, textStatus, errorThrown)
* - ajaxsubmitcomplete: function(event, jqXHR, textStatus)
*
*/
(function ($) {
var methods = {
init: function (options) {
var settings = $.extend({
action: null,
method: null,
data: null,
beforeSend: null,
success: null,
error: null,
complete: null
}, options);
return this.each(function () {
var $this = $(this);
$this.data("ajaxSubmit", $.extend({}, settings));
$this.bind("submit.ajaxSubmit", function (event) {
event.preventDefault();
methods.submit.call($this);
});
});
},
submit: function () {
return this.each(function () {
var $this = $(this),
settings = $this.data("ajaxSubmit"),
inputs = $this.find("input[name],select[name],textarea[name],button[name]"),
action = settings.action || $this.attr("action"),
method = settings.method || $this.attr("method") || "POST",
data = $.extend({}, settings.data);
inputs.each(function () {
var input = $(this);
data[input.attr("name")] = input.val();
});
$.ajax({
url: action,
type: method,
data: data,
beforeSend: function (jqXHR, ajaxSettings) {
var cancelled = false, event = $.Event("ajaxsubmitbeforesend");
if (typeof settings.beforeSend === "function") {
cancelled = settings.beforeSend.call($this, jqXHR, ajaxSettings) === false;
}
$this.trigger(event, [jqXHR, ajaxSettings, cancelled]);
cancelled = cancelled || event.isDefaultPrevented();
return !cancelled;
},
success: function (data, textStatus, jqXHR) {
if (typeof settings.success === "function") {
settings.success.call($this, data, textStatus, jqXHR);
}
$this.trigger("ajaxsubmitsuccess", [data, textStatus, jqXHR]);
},
error: function (jqXHR, textStatus, errorThrown) {
if (typeof settings.error === "function") {
settings.error.call($this, jqXHR, textStatus, errorThrown);
}
$this.trigger("ajaxsubmiterror", [jqXHR, textStatus, errorThrown]);
},
complete: function (jqXHR, textStatus) {
if (typeof settings.complete === "function") {
settings.complete.call($this, jqXHR, textStatus);
}
$this.trigger("ajaxsubmitcomplete", [jqXHR, textStatus]);
}
});
});
},
option: function (name, value) {
if (typeof name === "undefined") {
return this.data("ajaxSubmit");
}
else if (typeof name === "string" && typeof value === "undefined") {
return this.data("ajaxSubmit")[name];
}
return this.each(function () {
var $this = $(this), settings = $this.data("ajaxSubmit");
if (typeof name === "object") {
$.extend(settings, name);
}
else if (typeof name == "string") {
settings[name] = value;
}
});
},
destroy: function () {
return this.each(function () {
var $this = $(this);
$this.unbind(".ajaxSubmit");
$this.removeData("ajaxSubmit");
});
}
};
$.fn.ajaxSubmit = function (method) {
// Method calling logic
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.ajaxSubmit');
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment