diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/jquery.form.js | 90 |
1 files changed, 50 insertions, 40 deletions
diff --git a/lib/jquery.form.js b/lib/jquery.form.js index 329d47b9..c027f19c 100644 --- a/lib/jquery.form.js +++ b/lib/jquery.form.js @@ -1,14 +1,12 @@ /* * jQuery Form Plugin - * version: 2.17 (06-NOV-2008) + * version: 2.28 (10-MAY-2009) * @requires jQuery v1.2.2 or later * * Examples and documentation at: http://malsup.com/jquery/form/ * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html - * - * Revision: $Id$ */ ;(function($) { @@ -55,8 +53,15 @@ $.fn.ajaxSubmit = function(options) { if (typeof options == 'function') options = { success: options }; + var url = $.trim(this.attr('action')); + if (url) { + // clean url (don't include hash vaue) + url = (url.match(/^([^#]+)/)||[])[1]; + } + url = url || window.location.href || '' + options = $.extend({ - url: this.attr('action') || window.location.toString(), + url: url, type: this.attr('method') || 'GET' }, options || {}); @@ -81,7 +86,7 @@ $.fn.ajaxSubmit = function(options) { for (var n in options.data) { if(options.data[n] instanceof Array) { for (var k in options.data[n]) - a.push( { name: n, value: options.data[n][k] } ) + a.push( { name: n, value: options.data[n][k] } ); } else a.push( { name: n, value: options.data[n] } ); @@ -136,11 +141,15 @@ $.fn.ajaxSubmit = function(options) { if (files[j]) found = true; + var multipart = false; +// var mp = 'multipart/form-data'; +// multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp); + // options.iframe allows user to force iframe mode - if (options.iframe || found) { + if (options.iframe || found || multipart) { // hack to fix Safari hang (thanks to Tim Molendijk for this) // see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d - if ($.browser.safari && options.closeKeepAlive) + if (options.closeKeepAlive) $.get(options.closeKeepAlive, fileUpload); else fileUpload(); @@ -156,22 +165,19 @@ $.fn.ajaxSubmit = function(options) { // private function for handling file uploads (hat tip to YAHOO!) function fileUpload() { var form = $form[0]; - /* - if ($(':input[@name=submit]', form).length) { + + if ($(':input[name=submit]', form).length) { alert('Error: Form elements must not be named "submit".'); return; } - */ var opts = $.extend({}, $.ajaxSettings, options); - var s = jQuery.extend(true, {}, $.extend(true, {}, $.ajaxSettings), opts); + var s = $.extend(true, {}, $.extend(true, {}, $.ajaxSettings), opts); var id = 'jqFormIO' + (new Date().getTime()); - var $io = $('<iframe id="' + id + '" name="' + id + '" />'); + var $io = $('<iframe id="' + id + '" name="' + id + '" src="about:blank" />'); var io = $io[0]; - if ($.browser.msie || $.browser.opera) - io.src = 'javascript:false;document.write("");'; $io.css({ position: 'absolute', top: '-1000px', left: '-1000px' }); var xhr = { // mock object @@ -194,9 +200,9 @@ $.fn.ajaxSubmit = function(options) { if (g && ! $.active++) $.event.trigger("ajaxStart"); if (g) $.event.trigger("ajaxSend", [xhr, opts]); - if (s.beforeSend && s.beforeSend(xhr, s) === false) { - s.global && jQuery.active--; - return; + if (s.beforeSend && s.beforeSend(xhr, s) === false) { + s.global && $.active--; + return; } if (xhr.aborted) return; @@ -222,11 +228,13 @@ $.fn.ajaxSubmit = function(options) { setTimeout(function() { // make sure form attrs are set var t = $form.attr('target'), a = $form.attr('action'); - $form.attr({ - target: id, - method: 'POST', - action: opts.url - }); + + // update form attrs in IE friendly way + form.setAttribute('target',id); + if (form.getAttribute('method') != 'POST') + form.setAttribute('method', 'POST'); + if (form.getAttribute('action') != opts.url) + form.setAttribute('action', opts.url); // ie borks in some cases when setting encoding if (! options.skipEncodingOverride) { @@ -256,18 +264,19 @@ $.fn.ajaxSubmit = function(options) { } finally { // reset attrs and remove "extra" input elements - $form.attr('action', a); - t ? $form.attr('target', t) : $form.removeAttr('target'); + form.setAttribute('action',a); + t ? form.setAttribute('target', t) : $form.removeAttr('target'); $(extraInputs).remove(); } }, 10); + var nullCheckFlag = 0; + function cb() { if (cbInvoked++) return; io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false); - var operaHack = 0; var ok = true; try { if (timedOut) throw 'timeout'; @@ -276,10 +285,10 @@ $.fn.ajaxSubmit = function(options) { doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document; - if (doc.body == null && !operaHack && $.browser.opera) { - // In Opera 9.2.x the iframe DOM is not always traversable when - // the onload callback fires so we give Opera 100ms to right itself - operaHack = 1; + if ((doc.body == null || doc.body.innerHTML == '') && !nullCheckFlag) { + // in some browsers (cough, Opera 9.2.x) the iframe DOM is not always traversable when + // the onload callback fires, so we give them a 2nd chance + nullCheckFlag = 1; cbInvoked--; setTimeout(cb, 100); return; @@ -412,8 +421,10 @@ $.fn.formToArray = function(semantic) { if (semantic && form.clk && el.type == "image") { // handle image inputs on the fly when semantic == true - if(!el.disabled && form.clk == el) + if(!el.disabled && form.clk == el) { + a.push({name: n, value: $(el).val()}); a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); + } continue; } @@ -427,13 +438,11 @@ $.fn.formToArray = function(semantic) { } if (!semantic && form.clk) { - // input type=='image' are not found in elements array! handle them here - var inputs = form.getElementsByTagName("input"); - for(var i=0, max=inputs.length; i < max; i++) { - var input = inputs[i]; - var n = input.name; - if(n && !input.disabled && input.type == "image" && form.clk == input) - a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); + // input type=='image' are not found in elements array! handle it here + var $input = $(form.clk), input = $input[0], n = input.name; + if (n && !input.disabled && input.type == 'image') { + a.push({name: n, value: $input.val()}); + a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); } } return a; @@ -540,8 +549,9 @@ $.fieldValue = function(el, successful) { for(var i=(one ? index : 0); i < max; i++) { var op = ops[i]; if (op.selected) { - // extra pain for IE... - var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value; + var v = op.value; + if (!v) // extra pain for IE... + v = (op.attributes && op.attributes['value'] && !(op.attributes['value'].specified)) ? op.text : op.value; if (one) return v; a.push(v); } @@ -598,7 +608,7 @@ $.fn.resetForm = function() { $.fn.enable = function(b) { if (b == undefined) b = true; return this.each(function() { - this.disabled = !b + this.disabled = !b; }); }; |