2 * A method for submitting an HTML form using AJAX, as opposed to the
\r
3 * standard page-load way.
\r
5 * This method attempts to mimic the functionality of the original form
\r
6 * as best as possible (duplicating the method, action, and exact contents
\r
9 * There are three different resulting operations that can occur, after
\r
10 * your form has been submitted.
\r
12 * 1. The form is submitted and a callback is fired, letting you know
\r
14 * $("form").ajaxSubmit(function(){
\r
15 * alert("all done!");
\r
18 * 2. The form is submitted and the resulting HTML contents are injected
\r
19 * into the page, at your specified location.
\r
20 * $("form").ajaxSubmit("#destination");
\r
22 * 3. The form is submitted and the results returned from the server are
\r
23 * automatically executed (useful for having the server return more
\r
24 * Javascript commands to execute).
\r
25 * $("form").ajaxSubmit();
\r
27 * Additionally, an optional pre-submit callback can be provided. If it,
\r
28 * when called with the contents of the form, returns false, the form will
\r
31 * Finally, both the URL and method of the form submission can be
\r
32 * overidden using the 'url' and 'mth' arguments.
\r
34 * @param target arg for the target id element to render
\r
35 * @param post_cb callback after any results are returned
\r
36 * @param pre_cb callback function before submission
\r
37 * @param url form action override
\r
38 * @param mth form method override
\r
39 * @return "this" object
\r
40 * @see ajaxForm(), serialize(), load(), $.ajax()
\r
41 * @author Mark Constable (markc@renta.net)
\r
42 * @author G. vd Hoven, Mike Alsup, Sam Collett, John Resig
\r
44 $.fn.ajaxSubmit = function(target, post_cb, pre_cb, url, mth) {
\r
45 if ( !this.vars ) this.serialize();
\r
47 if (pre_cb && pre_cb.constructor == Function)
\r
48 if (pre_cb(this.vars) === false) return;
\r
50 var f = this.get(0);
\r
51 var url = url || f.action || '';
\r
52 var mth = mth || f.method || 'POST';
\r
54 if (target && target.constructor == Function) {
\r
55 $.ajax(mth, url, $.param(this.vars), target);
\r
56 } else if (target && target.constructor == String) {
\r
57 $(target).load(url, this.vars, post_cb);
\r
59 this.vars.push({name: 'evaljs', value: 1});
\r
60 $.ajax(mth, url, $.param(this.vars), function(r) {
\r
61 eval(r.responseText);
\r
69 * This function can be used to turn any HTML form into a form
\r
70 * that submits using AJAX only.
\r
72 * The purpose of using this method, instead of the ajaxSubmit()
\r
73 * and submit() methods, is to make absolutely sure that the
\r
74 * coordinates of <input type="image"/> elements are transmitted
\r
75 * correctly OR figuring out exactly which <input type="submit"/>
\r
76 * element was clicked to submit the form.
\r
78 * If neither of the above points are important to you, then you'll
\r
79 * probably just want to stick with the simpler ajaxSubmit() function.
\r
81 * Usage examples, similar to ajaxSubmit():
\r
83 * 1. Just eval the results returned from the backend.
\r
84 * $('#form-id').ajaxForm();
\r
86 * 2. Render backend results directly to target ID (expects (x)HTML).
\r
87 * $('#form-id').ajaxForm('#target-id');
\r
89 * 3. Submit to backend URL (form action) then call this function.
\r
90 * $('#form-id').ajaxForm(post_callback);
\r
92 * 4. Load target ID with backend results then call a function.
\r
93 * $('#form-id').ajaxForm('#target-id', post_callback);
\r
95 * 5. Call a browser function (for validation) and then (optionally)
\r
96 * load server results to target ID.
\r
97 * $('#form-id').ajaxForm('#target-id', null, pre_callback);
\r
99 * 6. Call validation function first then load server results to
\r
100 * target ID and then also call a browser function.
\r
101 * $('#form-id').ajaxForm('#target-id', post_callback, pre_callback);
\r
103 * @param target arg for the target id element to render
\r
104 * @param post_cb callback after any results are returned
\r
105 * @param pre_cb callback function before submission
\r
106 * @return the jQuery Object
\r
107 * @see serialize(), ajaxSubmit()
\r
108 * @author Mark Constable (markc@renta.net)
\r
109 * @author G. vd Hoven, Mike Alsup, Sam Collett, John Resig
\r
111 $.fn.ajaxForm = function(target, post_cb, pre_cb) {
\r
112 return this.each(function(){
\r
113 $('input[@type="submit"],input[@type="image"]', this).click(function(ev){
\r
114 this.form.clicked = this;
\r
115 if (ev.offsetX != undefined) {
\r
116 this.form.clicked_x = ev.offsetX;
\r
117 this.form.clicked_y = ev.offsetY;
\r
119 this.form.clicked_x = ev.pageX - this.offsetLeft;
\r
120 this.form.clicked_y = ev.pageY - this.offsetTop;
\r
123 }).submit(function(e){
\r
124 e.preventDefault();
\r
125 $(this).ajaxSubmit(target, post_cb, pre_cb);
\r
131 * A simple wrapper function that sits around the .serialize()
\r
132 * method, allowing you to easily extract the data stored within
\r
137 * 1. Serialize the contents of a form to a & and = delmited string:
\r
138 * $.param( $("form").formdata() );
\r
140 * @return An array of name/value pairs representing the form
\r
142 # @author John Resig
\r
144 $.fn.formdata = function(){
\r
150 * This function gathers form element variables into an array that
\r
151 * is embedded into the current "this" variable as "this.vars". It
\r
152 * is normally used in conjunction with formdata() or ajaxSubmit() but can
\r
153 * be used standalone as long as you don't need the x and y coordinates
\r
154 * associated with an <input type="image"/> element..
\r
156 * Standalone usage examples:
\r
158 * 1. Gather form vars and return array to LHS variable.
\r
159 * var myform = $('#form-id').serialize();
\r
161 * 2. Provide a serialized URL-ready string (after 1. above).
\r
162 * var mystring = $.param(myform.vars);
\r
164 * 3. Gather form vars and send to RHS plugin via "this.vars".
\r
165 * $('#form-id').serialize().some_other_plugin();
\r
167 * @return the jQuery Object
\r
168 * @see ajaxForm(), ajaxSubmit()
\r
169 * @author Mark Constable (markc@renta.net)
\r
170 * @author G. vd Hoven, Mike Alsup, Sam Collett, John Resig
\r
172 $.fn.serialize = function() {
\r
174 var ok = {INPUT:true, TEXTAREA:true, OPTION:true};
\r
176 $('*', this).each(function() {
\r
177 if (this.disabled || this.type == 'reset' ||
\r
178 (this.type == 'checkbox' && !this.checked) ||
\r
179 (this.type == 'radio' && !this.checked)) return;
\r
181 if (this.type == 'submit' || this.type == 'image') {
\r
182 if (this.form.clicked != this) return;
\r
184 if (this.type == 'image') {
\r
185 if (this.form.clicked_x) {
\r
186 a.push({name: this.name+'_x', value: this.form.clicked_x});
\r
187 a.push({name: this.name+'_y', value: this.form.clicked_y});
\r
193 if (!ok[this.nodeName.toUpperCase()])
\r
196 var par = this.parentNode;
\r
197 var p = par.nodeName.toUpperCase();
\r
198 if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected) return;
\r
200 var n = this.name || par.name;
\r
201 if (!n && p == 'OPTGROUP' && (par = par.parentNode))
\r
204 if (n == undefined) return;
\r
206 a.push({name: n, value: this.value});
\r