-
Notifications
You must be signed in to change notification settings - Fork 2
/
tinyAjaxForm.js
54 lines (44 loc) · 1.35 KB
/
tinyAjaxForm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* tinyAjaxForm.js - By Erez Shinan */
;(function($) {
"use strict";
$.fn.tinyAjaxForm = function(opts) {
// fail if nothing selected
if (!this.length || this.length > 1) {
console.log('tinyAjaxForm: skipping - no element selected');
return this;
}
if (typeof opts == 'function' || (opts && opts.selector)) {
opts = { success: opts };
}
opts = $.extend({
success: $.noop,
error: $.noop,
dataType: 'text'
}, opts);
if (opts.success && opts.success.selector) {
var success_obj = opts.success;
opts.success = function(res) {
success_obj.html(res);
}
}
$(this.selector).on('submit', function(event) {
event.preventDefault();
var $this = $(this);
var params = $this.serializeArray();
// Callback before submit; may cancel the submit or alter params
if (opts.beforeSubmit && opts.beforeSubmit(params, this, opts) === false) {
console.log('tinyAjaxForm: aborting because beforeSubmit returned false');
return this;
}
$.ajax({
type: this.method,
url: this.action,
data: $.param(params),
dataType: opts.dataType,
error: opts.error,
success: opts.success
});
return false;
});
}
})(jQuery);