3 * version: 2.02 (12/16/2007)
4 * @requires jQuery v1.1 or later
6 * Examples at: http://malsup.com/jquery/form/
7 * Dual licensed under the MIT and GPL licenses:
8 * http://www.opensource.org/licenses/mit-license.php
9 * http://www.gnu.org/licenses/gpl.html
15 * ajaxSubmit() provides a mechanism for submitting an HTML form using AJAX.
17 * ajaxSubmit accepts a single argument which can be either a success callback function
18 * or an options Object. If a function is provided it will be invoked upon successful
19 * completion of the submit and will be passed the response from the server.
20 * If an options Object is provided, the following attributes are supported:
22 * target: Identifies the element(s) in the page to be updated with the server response.
23 * This value may be specified as a jQuery selection string, a jQuery object,
27 * url: URL to which the form data will be submitted.
28 * default value: value of form's 'action' attribute
30 * type: The method in which the form data should be submitted, 'GET' or 'POST'.
31 * default value: value of form's 'method' attribute (or 'GET' if none found)
33 * data: Additional data to add to the request, specified as key/value pairs (see $.ajax).
35 * beforeSubmit: Callback method to be invoked before the form is submitted.
38 * success: Callback method to be invoked after the form has been successfully submitted
39 * and the response has been returned from the server
42 * dataType: Expected dataType of the response. One of: null, 'xml', 'script', or 'json'
45 * semantic: Boolean flag indicating whether data must be submitted in semantic order (slower).
46 * default value: false
48 * resetForm: Boolean flag indicating whether the form should be reset if the submit is successful
50 * clearForm: Boolean flag indicating whether the form should be cleared if the submit is successful
53 * The 'beforeSubmit' callback can be provided as a hook for running pre-submit logic or for
54 * validating the form data. If the 'beforeSubmit' callback returns false then the form will
55 * not be submitted. The 'beforeSubmit' callback is invoked with three arguments: the form data
56 * in array format, the jQuery object, and the options object passed into ajaxSubmit.
57 * The form data array takes the following form:
59 * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
61 * If a 'success' callback method is provided it is invoked after the response has been returned
62 * from the server. It is passed the responseText or responseXML value (depending on dataType).
63 * See jQuery.ajax for further details.
66 * The dataType option provides a means for specifying how the server response should be handled.
67 * This maps directly to the jQuery.httpData method. The following values are supported:
69 * 'xml': if dataType == 'xml' the server response is treated as XML and the 'success'
70 * callback method, if specified, will be passed the responseXML value
71 * 'json': if dataType == 'json' the server response will be evaluted and passed to
72 * the 'success' callback, if specified
73 * 'script': if dataType == 'script' the server response is evaluated in the global context
76 * Note that it does not make sense to use both the 'target' and 'dataType' options. If both
77 * are provided the target will be ignored.
79 * The semantic argument can be used to force form serialization in semantic order.
80 * This is normally true anyway, unless the form contains input elements of type='image'.
81 * If your form must be submitted with name/value pairs in semantic order and your form
82 * contains an input of type='image" then pass true for this arg, otherwise pass false
83 * (or nothing) to avoid the overhead for this logic.
86 * When used on its own, ajaxSubmit() is typically bound to a form's submit event like this:
88 * $("#form-id").submit(function() {
89 * $(this).ajaxSubmit(options);
90 * return false; // cancel conventional submit
93 * When using ajaxForm(), however, this is done for you.
96 * $('#myForm').ajaxSubmit(function(data) {
97 * alert('Form submit succeeded! Server returned: ' + data);
99 * @desc Submit form and alert server response
104 * target: '#myTargetDiv'
106 * $('#myForm').ajaxSubmit(options);
107 * @desc Submit form and update page element with server response
112 * success: function(responseText) {
113 * alert(responseText);
116 * $('#myForm').ajaxSubmit(options);
117 * @desc Submit form and alert the server response
122 * beforeSubmit: function(formArray, jqForm) {
123 * if (formArray.length == 0) {
124 * alert('Please enter data.');
129 * $('#myForm').ajaxSubmit(options);
130 * @desc Pre-submit validation which aborts the submit operation if form data is empty
135 * url: myJsonUrl.php,
137 * success: function(data) {
138 * // 'data' is an object representing the the evaluated json data
141 * $('#myForm').ajaxSubmit(options);
142 * @desc json data returned and evaluated
149 * success: function(responseXML) {
150 * // responseXML is XML document object
151 * var data = $('myElement', responseXML).text();
154 * $('#myForm').ajaxSubmit(options);
155 * @desc XML data returned from server
162 * $('#myForm').ajaxSubmit(options);
163 * @desc submit form and reset it if successful
166 * $('#myForm).submit(function() {
167 * $(this).ajaxSubmit();
170 * @desc Bind form's submit event to use ajaxSubmit
175 * @param options object literal containing options which control the form submission process
179 $.fn.ajaxSubmit = function(options) {
180 if (typeof options == 'function')
181 options = { success: options };
184 url: this.attr('action') || window.location.toString(),
185 type: this.attr('method') || 'GET'
188 // hook for manipulating the form data before it is extracted;
189 // convenient for use with rich editors like tinyMCE or FCKEditor
191 $.event.trigger('form.pre.serialize', [this, options, veto]);
192 if (veto.veto) return this;
194 var a = this.formToArray(options.semantic);
196 for (var n in options.data)
197 a.push( { name: n, value: options.data[n] } );
200 // give pre-submit callback an opportunity to abort the submit
201 if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) return this;
203 // fire vetoable 'validate' event
204 $.event.trigger('form.submit.validate', [a, this, options, veto]);
205 if (veto.veto) return this;
207 var q = $.param(a);//.replace(/%20/g,'+');
209 if (options.type.toUpperCase() == 'GET') {
210 options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q;
211 options.data = null; // data is null for 'get'
214 options.data = q; // data is the query string for 'post'
216 var $form = this, callbacks = [];
217 if (options.resetForm) callbacks.push(function() { $form.resetForm(); });
218 if (options.clearForm) callbacks.push(function() { $form.clearForm(); });
220 // perform a load on the target only if dataType is not provided
221 if (!options.dataType && options.target) {
222 var oldSuccess = options.success || function(){};
223 callbacks.push(function(data) {
224 if (this.evalScripts)
225 $(options.target).attr("innerHTML", data).evalScripts().each(oldSuccess, arguments);
226 else // jQuery v1.1.4
227 $(options.target).html(data).each(oldSuccess, arguments);
230 else if (options.success)
231 callbacks.push(options.success);
233 options.success = function(data, status) {
234 for (var i=0, max=callbacks.length; i < max; i++)
235 callbacks[i](data, status, $form);
238 // are there files to upload?
239 var files = $('input:file', this).fieldValue();
241 for (var j=0; j < files.length; j++)
245 // options.iframe allows user to force iframe mode
246 if (options.iframe || found) {
247 // hack to fix Safari hang (thanks to Tim Molendijk for this)
248 // see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d
249 if ($.browser.safari && options.closeKeepAlive)
250 $.get(options.closeKeepAlive, fileUpload);
257 // fire 'notify' event
258 $.event.trigger('form.submit.notify', [this, options]);
262 // private function for handling file uploads (hat tip to YAHOO!)
263 function fileUpload() {
265 var opts = $.extend({}, $.ajaxSettings, options);
267 var id = 'jqFormIO' + $.fn.ajaxSubmit.counter++;
268 var $io = $('<iframe id="' + id + '" name="' + id + '" />');
270 var op8 = $.browser.opera && window.opera.version() < 9;
271 if ($.browser.msie || op8) io.src = 'javascript:false;document.write("");';
272 $io.css({ position: 'absolute', top: '-1000px', left: '-1000px' });
274 var xhr = { // mock object
279 getAllResponseHeaders: function() {},
280 getResponseHeader: function() {},
281 setRequestHeader: function() {}
285 // trigger ajax global events so that activity/block indicators work like normal
286 if (g && ! $.active++) $.event.trigger("ajaxStart");
287 if (g) $.event.trigger("ajaxSend", [xhr, opts]);
292 // take a breath so that pending repaints get some cpu time before the upload starts
293 setTimeout(function() {
294 // make sure form attrs are set
295 var encAttr = form.encoding ? 'encoding' : 'enctype';
296 var t = $form.attr('target');
302 form[encAttr] = 'multipart/form-data';
306 setTimeout(function() { timedOut = true; cb(); }, opts.timeout);
308 // add iframe to doc and submit the form
309 $io.appendTo('body');
310 io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false);
312 $form.attr('target', t); // reset target
316 if (cbInvoked++) return;
318 io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false);
322 if (timedOut) throw 'timeout';
323 // extract the server response from the iframe
325 doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document;
326 xhr.responseText = doc.body ? doc.body.innerHTML : null;
327 xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc;
329 if (opts.dataType == 'json' || opts.dataType == 'script') {
330 var ta = doc.getElementsByTagName('textarea')[0];
331 data = ta ? ta.value : xhr.responseText;
332 if (opts.dataType == 'json')
333 eval("data = " + data);
337 else if (opts.dataType == 'xml') {
338 data = xhr.responseXML;
339 if (!data && xhr.responseText != null)
340 data = toXml(xhr.responseText);
343 data = xhr.responseText;
348 $.handleError(opts, xhr, 'error', e);
351 // ordering of these callbacks/triggers is odd, but that's how $.ajax does it
353 opts.success(data, 'success');
354 if (g) $.event.trigger("ajaxSuccess", [xhr, opts]);
356 if (g) $.event.trigger("ajaxComplete", [xhr, opts]);
357 if (g && ! --$.active) $.event.trigger("ajaxStop");
358 if (opts.complete) opts.complete(xhr, ok ? 'success' : 'error');
361 setTimeout(function() {
363 xhr.responseXML = null;
367 function toXml(s, doc) {
368 if (window.ActiveXObject) {
369 doc = new ActiveXObject('Microsoft.XMLDOM');
374 doc = (new DOMParser()).parseFromString(s, 'text/xml');
375 return (doc && doc.documentElement && doc.documentElement.tagName != 'parsererror') ? doc : null;
379 $.fn.ajaxSubmit.counter = 0; // used to create unique iframe ids
382 * ajaxForm() provides a mechanism for fully automating form submission.
384 * The advantages of using this method instead of ajaxSubmit() are:
386 * 1: This method will include coordinates for <input type="image" /> elements (if the element
387 * is used to submit the form).
388 * 2. This method will include the submit element's name/value data (for the element that was
389 * used to submit the form).
390 * 3. This method binds the submit() method to the form for you.
392 * Note that for accurate x/y coordinates of image submit elements in all browsers
393 * you need to also use the "dimensions" plugin (this method will auto-detect its presence).
395 * The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely
396 * passes the options argument along after properly binding events for submit elements and
397 * the form itself. See ajaxSubmit for a full description of the options argument.
402 * target: '#myTargetDiv'
404 * $('#myForm').ajaxSForm(options);
405 * @desc Bind form's submit event so that 'myTargetDiv' is updated with the server response
406 * when the form is submitted.
411 * success: function(responseText) {
412 * alert(responseText);
415 * $('#myForm').ajaxSubmit(options);
416 * @desc Bind form's submit event so that server response is alerted after the form is submitted.
421 * beforeSubmit: function(formArray, jqForm) {
422 * if (formArray.length == 0) {
423 * alert('Please enter data.');
428 * $('#myForm').ajaxSubmit(options);
429 * @desc Bind form's submit event so that pre-submit callback is invoked before the form
434 * @param options object literal containing options which control the form submission process
439 $.fn.ajaxForm = function(options) {
440 return this.ajaxFormUnbind().submit(submitHandler).each(function() {
441 // store options in hash
442 this.formPluginId = $.fn.ajaxForm.counter++;
443 $.fn.ajaxForm.optionHash[this.formPluginId] = options;
444 $(":submit,input:image", this).click(clickHandler);
448 $.fn.ajaxForm.counter = 1;
449 $.fn.ajaxForm.optionHash = {};
451 function clickHandler(e) {
452 var $form = this.form;
454 if (this.type == 'image') {
455 if (e.offsetX != undefined) {
456 $form.clk_x = e.offsetX;
457 $form.clk_y = e.offsetY;
458 } else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin
459 var offset = $(this).offset();
460 $form.clk_x = e.pageX - offset.left;
461 $form.clk_y = e.pageY - offset.top;
463 $form.clk_x = e.pageX - this.offsetLeft;
464 $form.clk_y = e.pageY - this.offsetTop;
468 setTimeout(function() { $form.clk = $form.clk_x = $form.clk_y = null; }, 10);
471 function submitHandler() {
472 // retrieve options from hash
473 var id = this.formPluginId;
474 var options = $.fn.ajaxForm.optionHash[id];
475 $(this).ajaxSubmit(options);
480 * ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm
482 * @name ajaxFormUnbind
487 $.fn.ajaxFormUnbind = function() {
488 this.unbind('submit', submitHandler);
489 return this.each(function() {
490 $(":submit,input:image", this).unbind('click', clickHandler);
496 * formToArray() gathers form element data into an array of objects that can
497 * be passed to any of the following ajax functions: $.get, $.post, or load.
498 * Each object in the array has both a 'name' and 'value' property. An example of
499 * an array for a simple login form might be:
501 * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
503 * It is this array that is passed to pre-submit callback functions provided to the
504 * ajaxSubmit() and ajaxForm() methods.
506 * The semantic argument can be used to force form serialization in semantic order.
507 * This is normally true anyway, unless the form contains input elements of type='image'.
508 * If your form must be submitted with name/value pairs in semantic order and your form
509 * contains an input of type='image" then pass true for this arg, otherwise pass false
510 * (or nothing) to avoid the overhead for this logic.
512 * @example var data = $("#myForm").formToArray();
513 * $.post( "myscript.cgi", data );
514 * @desc Collect all the data from a form and submit it to the server.
517 * @param semantic true if serialization must maintain strict semantic ordering of elements (slower)
518 * @type Array<Object>
521 $.fn.formToArray = function(semantic) {
523 if (this.length == 0) return a;
526 var els = semantic ? form.getElementsByTagName('*') : form.elements;
528 for(var i=0, max=els.length; i < max; i++) {
533 if (semantic && form.clk && el.type == "image") {
534 // handle image inputs on the fly when semantic == true
535 if(!el.disabled && form.clk == el)
536 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
540 var v = $.fieldValue(el, true);
541 if (v && v.constructor == Array) {
542 for(var j=0, jmax=v.length; j < jmax; j++)
543 a.push({name: n, value: v[j]});
545 else if (v !== null && typeof v != 'undefined')
546 a.push({name: n, value: v});
549 if (!semantic && form.clk) {
550 // input type=='image' are not found in elements array! handle them here
551 var inputs = form.getElementsByTagName("input");
552 for(var i=0, max=inputs.length; i < max; i++) {
553 var input = inputs[i];
555 if(n && !input.disabled && input.type == "image" && form.clk == input)
556 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
564 * Serializes form data into a 'submittable' string. This method will return a string
565 * in the format: name1=value1&name2=value2
567 * The semantic argument can be used to force form serialization in semantic order.
568 * If your form must be submitted with name/value pairs in semantic order then pass
569 * true for this arg, otherwise pass false (or nothing) to avoid the overhead for
570 * this logic (which can be significant for very large forms).
572 * @example var data = $("#myForm").formSerialize();
573 * $.ajax('POST', "myscript.cgi", data);
574 * @desc Collect all the data from a form into a single string
576 * @name formSerialize
577 * @param semantic true if serialization must maintain strict semantic ordering of elements (slower)
581 $.fn.formSerialize = function(semantic) {
582 //hand off to jQuery.param for proper encoding
583 return $.param(this.formToArray(semantic));
588 * Serializes all field elements in the jQuery object into a query string.
589 * This method will return a string in the format: name1=value1&name2=value2
591 * The successful argument controls whether or not serialization is limited to
592 * 'successful' controls (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls).
593 * The default value of the successful argument is true.
595 * @example var data = $("input").formSerialize();
596 * @desc Collect the data from all successful input elements into a query string
598 * @example var data = $(":radio").formSerialize();
599 * @desc Collect the data from all successful radio input elements into a query string
601 * @example var data = $("#myForm :checkbox").formSerialize();
602 * @desc Collect the data from all successful checkbox input elements in myForm into a query string
604 * @example var data = $("#myForm :checkbox").formSerialize(false);
605 * @desc Collect the data from all checkbox elements in myForm (even the unchecked ones) into a query string
607 * @example var data = $(":input").formSerialize();
608 * @desc Collect the data from all successful input, select, textarea and button elements into a query string
610 * @name fieldSerialize
611 * @param successful true if only successful controls should be serialized (default is true)
615 $.fn.fieldSerialize = function(successful) {
617 this.each(function() {
620 var v = $.fieldValue(this, successful);
621 if (v && v.constructor == Array) {
622 for (var i=0,max=v.length; i < max; i++)
623 a.push({name: n, value: v[i]});
625 else if (v !== null && typeof v != 'undefined')
626 a.push({name: this.name, value: v});
628 //hand off to jQuery.param for proper encoding
634 * Returns the value(s) of the element in the matched set. For example, consider the following form:
637 * <input name="A" type="text" />
638 * <input name="A" type="text" />
639 * <input name="B" type="checkbox" value="B1" />
640 * <input name="B" type="checkbox" value="B2"/>
641 * <input name="C" type="radio" value="C1" />
642 * <input name="C" type="radio" value="C2" />
645 * var v = $(':text').fieldValue();
646 * // if no values are entered into the text inputs
648 * // if values entered into the text inputs are 'foo' and 'bar'
651 * var v = $(':checkbox').fieldValue();
652 * // if neither checkbox is checked
654 * // if both checkboxes are checked
657 * var v = $(':radio').fieldValue();
658 * // if neither radio is checked
660 * // if first radio is checked
663 * The successful argument controls whether or not the field element must be 'successful'
664 * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls).
665 * The default value of the successful argument is true. If this value is false the value(s)
666 * for each element is returned.
668 * Note: This method *always* returns an array. If no valid value can be determined the
669 * array will be empty, otherwise it will contain one or more values.
671 * @example var data = $("#myPasswordElement").fieldValue();
673 * @desc Alerts the current value of the myPasswordElement element
675 * @example var data = $("#myForm :input").fieldValue();
676 * @desc Get the value(s) of the form elements in myForm
678 * @example var data = $("#myForm :checkbox").fieldValue();
679 * @desc Get the value(s) for the successful checkbox element(s) in the jQuery object.
681 * @example var data = $("#mySingleSelect").fieldValue();
682 * @desc Get the value(s) of the select control
684 * @example var data = $(':text').fieldValue();
685 * @desc Get the value(s) of the text input or textarea elements
687 * @example var data = $("#myMultiSelect").fieldValue();
688 * @desc Get the values for the select-multiple control
691 * @param Boolean successful true if only the values for successful controls should be returned (default is true)
692 * @type Array<String>
695 $.fn.fieldValue = function(successful) {
696 for (var val=[], i=0, max=this.length; i < max; i++) {
698 var v = $.fieldValue(el, successful);
699 if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length))
701 v.constructor == Array ? $.merge(val, v) : val.push(v);
707 * Returns the value of the field element.
709 * The successful argument controls whether or not the field element must be 'successful'
710 * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls).
711 * The default value of the successful argument is true. If the given element is not
712 * successful and the successful arg is not false then the returned value will be null.
714 * Note: If the successful flag is true (default) but the element is not successful, the return will be null
715 * Note: The value returned for a successful select-multiple element will always be an array.
716 * Note: If the element has no value the return value will be undefined.
718 * @example var data = jQuery.fieldValue($("#myPasswordElement")[0]);
719 * @desc Gets the current value of the myPasswordElement element
722 * @param Element el The DOM element for which the value will be returned
723 * @param Boolean successful true if value returned must be for a successful controls (default is true)
724 * @type String or Array<String> or null or undefined
727 $.fieldValue = function(el, successful) {
728 var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
729 if (typeof successful == 'undefined') successful = true;
731 if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
732 (t == 'checkbox' || t == 'radio') && !el.checked ||
733 (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
734 tag == 'select' && el.selectedIndex == -1))
737 if (tag == 'select') {
738 var index = el.selectedIndex;
739 if (index < 0) return null;
740 var a = [], ops = el.options;
741 var one = (t == 'select-one');
742 var max = (one ? index+1 : ops.length);
743 for(var i=(one ? index : 0); i < max; i++) {
746 // extra pain for IE...
747 var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value;
759 * Clears the form data. Takes the following actions on the form's input fields:
760 * - input text fields will have their 'value' property set to the empty string
761 * - select elements will have their 'selectedIndex' property set to -1
762 * - checkbox and radio inputs will have their 'checked' property set to false
763 * - inputs of type submit, button, reset, and hidden will *not* be effected
764 * - button elements will *not* be effected
766 * @example $('form').clearForm();
767 * @desc Clears all forms on the page.
773 $.fn.clearForm = function() {
774 return this.each(function() {
775 $('input,select,textarea', this).clearFields();
780 * Clears the selected form elements. Takes the following actions on the matched elements:
781 * - input text fields will have their 'value' property set to the empty string
782 * - select elements will have their 'selectedIndex' property set to -1
783 * - checkbox and radio inputs will have their 'checked' property set to false
784 * - inputs of type submit, button, reset, and hidden will *not* be effected
785 * - button elements will *not* be effected
787 * @example $('.myInputs').clearFields();
788 * @desc Clears all inputs with class myInputs
794 $.fn.clearFields = $.fn.clearInputs = function() {
795 return this.each(function() {
796 var t = this.type, tag = this.tagName.toLowerCase();
797 if (t == 'text' || t == 'password' || tag == 'textarea')
799 else if (t == 'checkbox' || t == 'radio')
800 this.checked = false;
801 else if (tag == 'select')
802 this.selectedIndex = -1;
808 * Resets the form data. Causes all form elements to be reset to their original value.
810 * @example $('form').resetForm();
811 * @desc Resets all forms on the page.
817 $.fn.resetForm = function() {
818 return this.each(function() {
819 // guard against an input with the name of 'reset'
820 // note that IE reports the reset function as an 'object'
821 if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
828 * Enables or disables any matching elements.
830 * @example $(':radio').enabled(false);
831 * @desc Disables all radio buttons
837 $.fn.enable = function(b) {
838 if (b == undefined) b = true;
839 return this.each(function() {
845 * Checks/unchecks any matching checkboxes or radio buttons and
846 * selects/deselects and matching option elements.
848 * @example $(':checkbox').selected();
849 * @desc Checks all checkboxes
855 $.fn.select = function(select) {
856 if (select == undefined) select = true;
857 return this.each(function() {
859 if (t == 'checkbox' || t == 'radio')
860 this.checked = select;
861 else if (this.tagName.toLowerCase() == 'option') {
862 var $sel = $(this).parent('select');
863 if (select && $sel[0] && $sel[0].type == 'select-one') {
864 // deselect all other options
865 $sel.find('option').select(false);
867 this.selected = select;