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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/**
* A method for submitting an HTML form using AJAX, as opposed to the
* standard page-load way.
*
* This method attempts to mimic the functionality of the original form
* as best as possible (duplicating the method, action, and exact contents
* of the form).
*
* There are three different resulting operations that can occur, after
* your form has been submitted.
*
* 1. The form is submitted and a callback is fired, letting you know
* when it's done:
* $("form").ajaxSubmit(function(){
* alert("all done!");
* });
*
* 2. The form is submitted and the resulting HTML contents are injected
* into the page, at your specified location.
* $("form").ajaxSubmit("#destination");
*
* 3. The form is submitted and the results returned from the server are
* automatically executed (useful for having the server return more
* Javascript commands to execute).
* $("form").ajaxSubmit();
*
* Additionally, an optional pre-submit callback can be provided. If it,
* when called with the contents of the form, returns false, the form will
* not be submitted.
*
* Finally, both the URL and method of the form submission can be
* overidden using the 'url' and 'mth' arguments.
*
* @param target arg for the target id element to render
* @param post_cb callback after any results are returned
* @param pre_cb callback function before submission
* @param url form action override
* @param mth form method override
* @return "this" object
* @see ajaxForm(), serialize(), load(), $.ajax()
* @author Mark Constable (markc@renta.net)
* @author G. vd Hoven, Mike Alsup, Sam Collett, John Resig
*/
$.fn.ajaxSubmit = function(target, post_cb, pre_cb, url, mth) {
if ( !this.vars ) this.serialize();
if (pre_cb && pre_cb.constructor == Function)
if (pre_cb(this.vars) === false) return;
var f = this.get(0);
var url = url || f.action || '';
var mth = mth || f.method || 'POST';
if (target && target.constructor == Function) {
$.ajax(mth, url, $.param(this.vars), target);
} else if (target && target.constructor == String) {
$(target).load(url, this.vars, post_cb);
} else {
this.vars.push({name: 'evaljs', value: 1});
$.ajax(mth, url, $.param(this.vars), function(r) {
eval(r.responseText);
});
}
return this;
};
/**
* This function can be used to turn any HTML form into a form
* that submits using AJAX only.
*
* The purpose of using this method, instead of the ajaxSubmit()
* and submit() methods, is to make absolutely sure that the
* coordinates of <input type="image"/> elements are transmitted
* correctly OR figuring out exactly which <input type="submit"/>
* element was clicked to submit the form.
*
* If neither of the above points are important to you, then you'll
* probably just want to stick with the simpler ajaxSubmit() function.
*
* Usage examples, similar to ajaxSubmit():
*
* 1. Just eval the results returned from the backend.
* $('#form-id').ajaxForm();
*
* 2. Render backend results directly to target ID (expects (x)HTML).
* $('#form-id').ajaxForm('#target-id');
*
* 3. Submit to backend URL (form action) then call this function.
* $('#form-id').ajaxForm(post_callback);
*
* 4. Load target ID with backend results then call a function.
* $('#form-id').ajaxForm('#target-id', post_callback);
*
* 5. Call a browser function (for validation) and then (optionally)
* load server results to target ID.
* $('#form-id').ajaxForm('#target-id', null, pre_callback);
*
* 6. Call validation function first then load server results to
* target ID and then also call a browser function.
* $('#form-id').ajaxForm('#target-id', post_callback, pre_callback);
*
* @param target arg for the target id element to render
* @param post_cb callback after any results are returned
* @param pre_cb callback function before submission
* @return the jQuery Object
* @see serialize(), ajaxSubmit()
* @author Mark Constable (markc@renta.net)
* @author G. vd Hoven, Mike Alsup, Sam Collett, John Resig
*/
$.fn.ajaxForm = function(target, post_cb, pre_cb) {
return this.each(function(){
$('input[@type="submit"],input[@type="image"]', this).click(function(ev){
this.form.clicked = this;
if (ev.offsetX != undefined) {
this.form.clicked_x = ev.offsetX;
this.form.clicked_y = ev.offsetY;
} else {
this.form.clicked_x = ev.pageX - this.offsetLeft;
this.form.clicked_y = ev.pageY - this.offsetTop;
}
});
}).submit(function(e){
e.preventDefault();
$(this).ajaxSubmit(target, post_cb, pre_cb);
return false;
});
};
/*
$.ajax({
type: "POST",
url: "foo.cgi",
data: $.param( $("form").formdata() ),
success: function(){},
error: function(){},
complete: function(){}
});
*/
/**
* A simple wrapper function that sits around the .serialize()
* method, allowing you to easily extract the data stored within
* a form.
*
* Usage examples:
*
* 1. Serialize the contents of a form to a & and = delmited string:
* $.param( $("form").formdata() );
*
* @return An array of name/value pairs representing the form
* @see serialize()
# @author John Resig
*/
$.fn.formdata = function(){
this.serialize();
return this.vars;
};
/**
* This function gathers form element variables into an array that
* is embedded into the current "this" variable as "this.vars". It
* is normally used in conjunction with formdata() or ajaxSubmit() but can
* be used standalone as long as you don't need the x and y coordinates
* associated with an <input type="image"/> element..
*
* Standalone usage examples:
*
* 1. Gather form vars and return array to LHS variable.
* var myform = $('#form-id').serialize();
*
* 2. Provide a serialized URL-ready string (after 1. above).
* var mystring = $.param(myform.vars);
*
* 3. Gather form vars and send to RHS plugin via "this.vars".
* $('#form-id').serialize().some_other_plugin();
*
* @return the jQuery Object
* @see ajaxForm(), ajaxSubmit()
* @author Mark Constable (markc@renta.net)
* @author G. vd Hoven, Mike Alsup, Sam Collett, John Resig
*/
$.fn.serialize = function() {
var a = [];
var ok = {INPUT:true, TEXTAREA:true, OPTION:true};
$('*', this).each(function() {
if (this.disabled || this.type == 'reset' ||
(this.type == 'checkbox' && !this.checked) ||
(this.type == 'radio' && !this.checked)) return;
if (this.type == 'submit' || this.type == 'image') {
if (this.form.clicked != this) return;
if (this.type == 'image') {
if (this.form.clicked_x) {
a.push({name: this.name+'_x', value: this.form.clicked_x});
a.push({name: this.name+'_y', value: this.form.clicked_y});
return;
}
}
}
if (!ok[this.nodeName.toUpperCase()])
return;
var par = this.parentNode;
var p = par.nodeName.toUpperCase();
if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected) return;
var n = this.name;
if (!n) n = (p == 'OPTGROUP') ? par.parentNode.name : (p == 'SELECT') ? par.name : this.name;
if (n == undefined) return;
a.push({name: n, value: this.value});
});
this.vars = a;
return this;
};
|