[69] | 1 | /* |
---|
| 2 | * jQuery Form Plugin |
---|
| 3 | * version: 2.01 (10/31/2007) |
---|
| 4 | * @requires jQuery v1.1 or later |
---|
| 5 | * |
---|
| 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 |
---|
| 10 | * |
---|
| 11 | * Revision: $Id: jquery.form.js 3767 2007-10-31 11:23:47Z malsup $ |
---|
| 12 | */ |
---|
| 13 | (function($) { |
---|
| 14 | /** |
---|
| 15 | * ajaxSubmit() provides a mechanism for submitting an HTML form using AJAX. |
---|
| 16 | * |
---|
| 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: |
---|
| 21 | * |
---|
| 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, |
---|
| 24 | * or a DOM element. |
---|
| 25 | * default value: null |
---|
| 26 | * |
---|
| 27 | * url: URL to which the form data will be submitted. |
---|
| 28 | * default value: value of form's 'action' attribute |
---|
| 29 | * |
---|
| 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) |
---|
| 32 | * |
---|
| 33 | * data: Additional data to add to the request, specified as key/value pairs (see $.ajax). |
---|
| 34 | * |
---|
| 35 | * beforeSubmit: Callback method to be invoked before the form is submitted. |
---|
| 36 | * default value: null |
---|
| 37 | * |
---|
| 38 | * success: Callback method to be invoked after the form has been successfully submitted |
---|
| 39 | * and the response has been returned from the server |
---|
| 40 | * default value: null |
---|
| 41 | * |
---|
| 42 | * dataType: Expected dataType of the response. One of: null, 'xml', 'script', or 'json' |
---|
| 43 | * default value: null |
---|
| 44 | * |
---|
| 45 | * semantic: Boolean flag indicating whether data must be submitted in semantic order (slower). |
---|
| 46 | * default value: false |
---|
| 47 | * |
---|
| 48 | * resetForm: Boolean flag indicating whether the form should be reset if the submit is successful |
---|
| 49 | * |
---|
| 50 | * clearForm: Boolean flag indicating whether the form should be cleared if the submit is successful |
---|
| 51 | * |
---|
| 52 | * |
---|
| 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: |
---|
| 58 | * |
---|
| 59 | * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] |
---|
| 60 | * |
---|
| 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. |
---|
| 64 | * |
---|
| 65 | * |
---|
| 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: |
---|
| 68 | * |
---|
| 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 |
---|
| 74 | * |
---|
| 75 | * |
---|
| 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. |
---|
| 78 | * |
---|
| 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. |
---|
| 84 | * |
---|
| 85 | * |
---|
| 86 | * When used on its own, ajaxSubmit() is typically bound to a form's submit event like this: |
---|
| 87 | * |
---|
| 88 | * $("#form-id").submit(function() { |
---|
| 89 | * $(this).ajaxSubmit(options); |
---|
| 90 | * return false; // cancel conventional submit |
---|
| 91 | * }); |
---|
| 92 | * |
---|
| 93 | * When using ajaxForm(), however, this is done for you. |
---|
| 94 | * |
---|
| 95 | * @example |
---|
| 96 | * $('#myForm').ajaxSubmit(function(data) { |
---|
| 97 | * alert('Form submit succeeded! Server returned: ' + data); |
---|
| 98 | * }); |
---|
| 99 | * @desc Submit form and alert server response |
---|
| 100 | * |
---|
| 101 | * |
---|
| 102 | * @example |
---|
| 103 | * var options = { |
---|
| 104 | * target: '#myTargetDiv' |
---|
| 105 | * }; |
---|
| 106 | * $('#myForm').ajaxSubmit(options); |
---|
| 107 | * @desc Submit form and update page element with server response |
---|
| 108 | * |
---|
| 109 | * |
---|
| 110 | * @example |
---|
| 111 | * var options = { |
---|
| 112 | * success: function(responseText) { |
---|
| 113 | * alert(responseText); |
---|
| 114 | * } |
---|
| 115 | * }; |
---|
| 116 | * $('#myForm').ajaxSubmit(options); |
---|
| 117 | * @desc Submit form and alert the server response |
---|
| 118 | * |
---|
| 119 | * |
---|
| 120 | * @example |
---|
| 121 | * var options = { |
---|
| 122 | * beforeSubmit: function(formArray, jqForm) { |
---|
| 123 | * if (formArray.length == 0) { |
---|
| 124 | * alert('Please enter data.'); |
---|
| 125 | * return false; |
---|
| 126 | * } |
---|
| 127 | * } |
---|
| 128 | * }; |
---|
| 129 | * $('#myForm').ajaxSubmit(options); |
---|
| 130 | * @desc Pre-submit validation which aborts the submit operation if form data is empty |
---|
| 131 | * |
---|
| 132 | * |
---|
| 133 | * @example |
---|
| 134 | * var options = { |
---|
| 135 | * url: myJsonUrl.php, |
---|
| 136 | * dataType: 'json', |
---|
| 137 | * success: function(data) { |
---|
| 138 | * // 'data' is an object representing the the evaluated json data |
---|
| 139 | * } |
---|
| 140 | * }; |
---|
| 141 | * $('#myForm').ajaxSubmit(options); |
---|
| 142 | * @desc json data returned and evaluated |
---|
| 143 | * |
---|
| 144 | * |
---|
| 145 | * @example |
---|
| 146 | * var options = { |
---|
| 147 | * url: myXmlUrl.php, |
---|
| 148 | * dataType: 'xml', |
---|
| 149 | * success: function(responseXML) { |
---|
| 150 | * // responseXML is XML document object |
---|
| 151 | * var data = $('myElement', responseXML).text(); |
---|
| 152 | * } |
---|
| 153 | * }; |
---|
| 154 | * $('#myForm').ajaxSubmit(options); |
---|
| 155 | * @desc XML data returned from server |
---|
| 156 | * |
---|
| 157 | * |
---|
| 158 | * @example |
---|
| 159 | * var options = { |
---|
| 160 | * resetForm: true |
---|
| 161 | * }; |
---|
| 162 | * $('#myForm').ajaxSubmit(options); |
---|
| 163 | * @desc submit form and reset it if successful |
---|
| 164 | * |
---|
| 165 | * @example |
---|
| 166 | * $('#myForm).submit(function() { |
---|
| 167 | * $(this).ajaxSubmit(); |
---|
| 168 | * return false; |
---|
| 169 | * }); |
---|
| 170 | * @desc Bind form's submit event to use ajaxSubmit |
---|
| 171 | * |
---|
| 172 | * |
---|
| 173 | * @name ajaxSubmit |
---|
| 174 | * @type jQuery |
---|
| 175 | * @param options object literal containing options which control the form submission process |
---|
| 176 | * @cat Plugins/Form |
---|
| 177 | * @return jQuery |
---|
| 178 | */ |
---|
| 179 | $.fn.ajaxSubmit = function(options) { |
---|
| 180 | if (typeof options == 'function') |
---|
| 181 | options = { success: options }; |
---|
| 182 | |
---|
| 183 | options = $.extend({ |
---|
| 184 | url: this.attr('action') || window.location.toString(), |
---|
| 185 | type: this.attr('method') || 'GET' |
---|
| 186 | }, options || {}); |
---|
| 187 | |
---|
| 188 | // hook for manipulating the form data before it is extracted; |
---|
| 189 | // convenient for use with rich editors like tinyMCE or FCKEditor |
---|
| 190 | var veto = {}; |
---|
| 191 | $.event.trigger('form.pre.serialize', [this, options, veto]); |
---|
| 192 | if (veto.veto) return this; |
---|
| 193 | |
---|
| 194 | var a = this.formToArray(options.semantic); |
---|
| 195 | if (options.data) { |
---|
| 196 | for (var n in options.data) |
---|
| 197 | a.push( { name: n, value: options.data[n] } ); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | // give pre-submit callback an opportunity to abort the submit |
---|
| 201 | if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) return this; |
---|
| 202 | |
---|
| 203 | // fire vetoable 'validate' event |
---|
| 204 | $.event.trigger('form.submit.validate', [a, this, options, veto]); |
---|
| 205 | if (veto.veto) return this; |
---|
| 206 | |
---|
| 207 | var q = $.param(a);//.replace(/%20/g,'+'); |
---|
| 208 | |
---|
| 209 | if (options.type.toUpperCase() == 'GET') { |
---|
| 210 | options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; |
---|
| 211 | options.data = null; // data is null for 'get' |
---|
| 212 | } |
---|
| 213 | else |
---|
| 214 | options.data = q; // data is the query string for 'post' |
---|
| 215 | |
---|
| 216 | var $form = this, callbacks = []; |
---|
| 217 | if (options.resetForm) callbacks.push(function() { $form.resetForm(); }); |
---|
| 218 | if (options.clearForm) callbacks.push(function() { $form.clearForm(); }); |
---|
| 219 | |
---|
| 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); |
---|
| 228 | }); |
---|
| 229 | } |
---|
| 230 | else if (options.success) |
---|
| 231 | callbacks.push(options.success); |
---|
| 232 | |
---|
| 233 | options.success = function(data, status) { |
---|
| 234 | for (var i=0, max=callbacks.length; i < max; i++) |
---|
| 235 | callbacks[i](data, status, $form); |
---|
| 236 | }; |
---|
| 237 | |
---|
| 238 | // are there files to upload? |
---|
| 239 | var files = $('input:file', this).fieldValue(); |
---|
| 240 | var found = false; |
---|
| 241 | for (var j=0; j < files.length; j++) |
---|
| 242 | if (files[j]) |
---|
| 243 | found = true; |
---|
| 244 | |
---|
| 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); |
---|
| 251 | else |
---|
| 252 | fileUpload(); |
---|
| 253 | } |
---|
| 254 | else |
---|
| 255 | $.ajax(options); |
---|
| 256 | |
---|
| 257 | // fire 'notify' event |
---|
| 258 | $.event.trigger('form.submit.notify', [this, options]); |
---|
| 259 | return this; |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | // private function for handling file uploads (hat tip to YAHOO!) |
---|
| 263 | function fileUpload() { |
---|
| 264 | var form = $form[0]; |
---|
| 265 | var opts = $.extend({}, $.ajaxSettings, options); |
---|
| 266 | |
---|
| 267 | var id = 'jqFormIO' + $.fn.ajaxSubmit.counter++; |
---|
| 268 | var $io = $('<iframe id="' + id + '" name="' + id + '" />'); |
---|
| 269 | var io = $io[0]; |
---|
| 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' }); |
---|
| 273 | |
---|
| 274 | var xhr = { // mock object |
---|
| 275 | responseText: null, |
---|
| 276 | responseXML: null, |
---|
| 277 | status: 0, |
---|
| 278 | statusText: 'n/a', |
---|
| 279 | getAllResponseHeaders: function() {}, |
---|
| 280 | getResponseHeader: function() {}, |
---|
| 281 | setRequestHeader: function() {} |
---|
| 282 | }; |
---|
| 283 | |
---|
| 284 | var g = opts.global; |
---|
| 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]); |
---|
| 288 | |
---|
| 289 | var cbInvoked = 0; |
---|
| 290 | var timedOut = 0; |
---|
| 291 | |
---|
| 292 | // take a breath so that pending repaints get some cpu time before the upload starts |
---|
| 293 | setTimeout(function() { |
---|
| 294 | $io.appendTo('body'); |
---|
| 295 | // jQuery's event binding doesn't work for iframe events in IE |
---|
| 296 | io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false); |
---|
| 297 | |
---|
| 298 | // make sure form attrs are set |
---|
| 299 | var encAttr = form.encoding ? 'encoding' : 'enctype'; |
---|
| 300 | var t = $form.attr('target'); |
---|
| 301 | $form.attr({ |
---|
| 302 | target: id, |
---|
| 303 | method: 'POST', |
---|
| 304 | action: opts.url |
---|
| 305 | }); |
---|
| 306 | form[encAttr] = 'multipart/form-data'; |
---|
| 307 | |
---|
| 308 | // support timout |
---|
| 309 | if (opts.timeout) |
---|
| 310 | setTimeout(function() { timedOut = true; cb(); }, opts.timeout); |
---|
| 311 | |
---|
| 312 | form.submit(); |
---|
| 313 | $form.attr('target', t); // reset target |
---|
| 314 | }, 10); |
---|
| 315 | |
---|
| 316 | function cb() { |
---|
| 317 | if (cbInvoked++) return; |
---|
| 318 | |
---|
| 319 | io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false); |
---|
| 320 | |
---|
| 321 | var ok = true; |
---|
| 322 | try { |
---|
| 323 | if (timedOut) throw 'timeout'; |
---|
| 324 | // extract the server response from the iframe |
---|
| 325 | var data, doc; |
---|
| 326 | doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document; |
---|
| 327 | xhr.responseText = doc.body ? doc.body.innerHTML : null; |
---|
| 328 | xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc; |
---|
| 329 | |
---|
| 330 | if (opts.dataType == 'json' || opts.dataType == 'script') { |
---|
| 331 | var ta = doc.getElementsByTagName('textarea')[0]; |
---|
| 332 | data = ta ? ta.value : xhr.responseText; |
---|
| 333 | if (opts.dataType == 'json') |
---|
| 334 | eval("data = " + data); |
---|
| 335 | else |
---|
| 336 | $.globalEval(data); |
---|
| 337 | } |
---|
| 338 | else if (opts.dataType == 'xml') { |
---|
| 339 | data = xhr.responseXML; |
---|
| 340 | if (!data && xhr.responseText != null) |
---|
| 341 | data = toXml(xhr.responseText); |
---|
| 342 | } |
---|
| 343 | else { |
---|
| 344 | data = xhr.responseText; |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | catch(e){ |
---|
| 348 | ok = false; |
---|
| 349 | $.handleError(opts, xhr, 'error', e); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | // ordering of these callbacks/triggers is odd, but that's how $.ajax does it |
---|
| 353 | if (ok) { |
---|
| 354 | opts.success(data, 'success'); |
---|
| 355 | if (g) $.event.trigger("ajaxSuccess", [xhr, opts]); |
---|
| 356 | } |
---|
| 357 | if (g) $.event.trigger("ajaxComplete", [xhr, opts]); |
---|
| 358 | if (g && ! --$.active) $.event.trigger("ajaxStop"); |
---|
| 359 | if (opts.complete) opts.complete(xhr, ok ? 'success' : 'error'); |
---|
| 360 | |
---|
| 361 | // clean up |
---|
| 362 | setTimeout(function() { |
---|
| 363 | $io.remove(); |
---|
| 364 | xhr.responseXML = null; |
---|
| 365 | }, 100); |
---|
| 366 | }; |
---|
| 367 | |
---|
| 368 | function toXml(s, doc) { |
---|
| 369 | if (window.ActiveXObject) { |
---|
| 370 | doc = new ActiveXObject('Microsoft.XMLDOM'); |
---|
| 371 | doc.async = 'false'; |
---|
| 372 | doc.loadXML(s); |
---|
| 373 | } |
---|
| 374 | else |
---|
| 375 | doc = (new DOMParser()).parseFromString(s, 'text/xml'); |
---|
| 376 | return (doc && doc.documentElement && doc.documentElement.tagName != 'parsererror') ? doc : null; |
---|
| 377 | }; |
---|
| 378 | }; |
---|
| 379 | }; |
---|
| 380 | $.fn.ajaxSubmit.counter = 0; // used to create unique iframe ids |
---|
| 381 | |
---|
| 382 | /** |
---|
| 383 | * ajaxForm() provides a mechanism for fully automating form submission. |
---|
| 384 | * |
---|
| 385 | * The advantages of using this method instead of ajaxSubmit() are: |
---|
| 386 | * |
---|
| 387 | * 1: This method will include coordinates for <input type="image" /> elements (if the element |
---|
| 388 | * is used to submit the form). |
---|
| 389 | * 2. This method will include the submit element's name/value data (for the element that was |
---|
| 390 | * used to submit the form). |
---|
| 391 | * 3. This method binds the submit() method to the form for you. |
---|
| 392 | * |
---|
| 393 | * Note that for accurate x/y coordinates of image submit elements in all browsers |
---|
| 394 | * you need to also use the "dimensions" plugin (this method will auto-detect its presence). |
---|
| 395 | * |
---|
| 396 | * The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely |
---|
| 397 | * passes the options argument along after properly binding events for submit elements and |
---|
| 398 | * the form itself. See ajaxSubmit for a full description of the options argument. |
---|
| 399 | * |
---|
| 400 | * |
---|
| 401 | * @example |
---|
| 402 | * var options = { |
---|
| 403 | * target: '#myTargetDiv' |
---|
| 404 | * }; |
---|
| 405 | * $('#myForm').ajaxSForm(options); |
---|
| 406 | * @desc Bind form's submit event so that 'myTargetDiv' is updated with the server response |
---|
| 407 | * when the form is submitted. |
---|
| 408 | * |
---|
| 409 | * |
---|
| 410 | * @example |
---|
| 411 | * var options = { |
---|
| 412 | * success: function(responseText) { |
---|
| 413 | * alert(responseText); |
---|
| 414 | * } |
---|
| 415 | * }; |
---|
| 416 | * $('#myForm').ajaxSubmit(options); |
---|
| 417 | * @desc Bind form's submit event so that server response is alerted after the form is submitted. |
---|
| 418 | * |
---|
| 419 | * |
---|
| 420 | * @example |
---|
| 421 | * var options = { |
---|
| 422 | * beforeSubmit: function(formArray, jqForm) { |
---|
| 423 | * if (formArray.length == 0) { |
---|
| 424 | * alert('Please enter data.'); |
---|
| 425 | * return false; |
---|
| 426 | * } |
---|
| 427 | * } |
---|
| 428 | * }; |
---|
| 429 | * $('#myForm').ajaxSubmit(options); |
---|
| 430 | * @desc Bind form's submit event so that pre-submit callback is invoked before the form |
---|
| 431 | * is submitted. |
---|
| 432 | * |
---|
| 433 | * |
---|
| 434 | * @name ajaxForm |
---|
| 435 | * @param options object literal containing options which control the form submission process |
---|
| 436 | * @return jQuery |
---|
| 437 | * @cat Plugins/Form |
---|
| 438 | * @type jQuery |
---|
| 439 | */ |
---|
| 440 | $.fn.ajaxForm = function(options) { |
---|
| 441 | return this.ajaxFormUnbind().submit(submitHandler).each(function() { |
---|
| 442 | // store options in hash |
---|
| 443 | this.formPluginId = $.fn.ajaxForm.counter++; |
---|
| 444 | $.fn.ajaxForm.optionHash[this.formPluginId] = options; |
---|
| 445 | $(":submit,input:image", this).click(clickHandler); |
---|
| 446 | }); |
---|
| 447 | }; |
---|
| 448 | |
---|
| 449 | $.fn.ajaxForm.counter = 1; |
---|
| 450 | $.fn.ajaxForm.optionHash = {}; |
---|
| 451 | |
---|
| 452 | function clickHandler(e) { |
---|
| 453 | var $form = this.form; |
---|
| 454 | $form.clk = this; |
---|
| 455 | if (this.type == 'image') { |
---|
| 456 | if (e.offsetX != undefined) { |
---|
| 457 | $form.clk_x = e.offsetX; |
---|
| 458 | $form.clk_y = e.offsetY; |
---|
| 459 | } else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin |
---|
| 460 | var offset = $(this).offset(); |
---|
| 461 | $form.clk_x = e.pageX - offset.left; |
---|
| 462 | $form.clk_y = e.pageY - offset.top; |
---|
| 463 | } else { |
---|
| 464 | $form.clk_x = e.pageX - this.offsetLeft; |
---|
| 465 | $form.clk_y = e.pageY - this.offsetTop; |
---|
| 466 | } |
---|
| 467 | } |
---|
| 468 | // clear form vars |
---|
| 469 | setTimeout(function() { $form.clk = $form.clk_x = $form.clk_y = null; }, 10); |
---|
| 470 | }; |
---|
| 471 | |
---|
| 472 | function submitHandler() { |
---|
| 473 | // retrieve options from hash |
---|
| 474 | var id = this.formPluginId; |
---|
| 475 | var options = $.fn.ajaxForm.optionHash[id]; |
---|
| 476 | $(this).ajaxSubmit(options); |
---|
| 477 | return false; |
---|
| 478 | }; |
---|
| 479 | |
---|
| 480 | /** |
---|
| 481 | * ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm |
---|
| 482 | * |
---|
| 483 | * @name ajaxFormUnbind |
---|
| 484 | * @return jQuery |
---|
| 485 | * @cat Plugins/Form |
---|
| 486 | * @type jQuery |
---|
| 487 | */ |
---|
| 488 | $.fn.ajaxFormUnbind = function() { |
---|
| 489 | this.unbind('submit', submitHandler); |
---|
| 490 | return this.each(function() { |
---|
| 491 | $(":submit,input:image", this).unbind('click', clickHandler); |
---|
| 492 | }); |
---|
| 493 | |
---|
| 494 | }; |
---|
| 495 | |
---|
| 496 | /** |
---|
| 497 | * formToArray() gathers form element data into an array of objects that can |
---|
| 498 | * be passed to any of the following ajax functions: $.get, $.post, or load. |
---|
| 499 | * Each object in the array has both a 'name' and 'value' property. An example of |
---|
| 500 | * an array for a simple login form might be: |
---|
| 501 | * |
---|
| 502 | * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] |
---|
| 503 | * |
---|
| 504 | * It is this array that is passed to pre-submit callback functions provided to the |
---|
| 505 | * ajaxSubmit() and ajaxForm() methods. |
---|
| 506 | * |
---|
| 507 | * The semantic argument can be used to force form serialization in semantic order. |
---|
| 508 | * This is normally true anyway, unless the form contains input elements of type='image'. |
---|
| 509 | * If your form must be submitted with name/value pairs in semantic order and your form |
---|
| 510 | * contains an input of type='image" then pass true for this arg, otherwise pass false |
---|
| 511 | * (or nothing) to avoid the overhead for this logic. |
---|
| 512 | * |
---|
| 513 | * @example var data = $("#myForm").formToArray(); |
---|
| 514 | * $.post( "myscript.cgi", data ); |
---|
| 515 | * @desc Collect all the data from a form and submit it to the server. |
---|
| 516 | * |
---|
| 517 | * @name formToArray |
---|
| 518 | * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) |
---|
| 519 | * @type Array<Object> |
---|
| 520 | * @cat Plugins/Form |
---|
| 521 | */ |
---|
| 522 | $.fn.formToArray = function(semantic) { |
---|
| 523 | var a = []; |
---|
| 524 | if (this.length == 0) return a; |
---|
| 525 | |
---|
| 526 | var form = this[0]; |
---|
| 527 | var els = semantic ? form.getElementsByTagName('*') : form.elements; |
---|
| 528 | if (!els) return a; |
---|
| 529 | for(var i=0, max=els.length; i < max; i++) { |
---|
| 530 | var el = els[i]; |
---|
| 531 | var n = el.name; |
---|
| 532 | if (!n) continue; |
---|
| 533 | |
---|
| 534 | if (semantic && form.clk && el.type == "image") { |
---|
| 535 | // handle image inputs on the fly when semantic == true |
---|
| 536 | if(!el.disabled && form.clk == el) |
---|
| 537 | a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
---|
| 538 | continue; |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | var v = $.fieldValue(el, true); |
---|
| 542 | if (v && v.constructor == Array) { |
---|
| 543 | for(var j=0, jmax=v.length; j < jmax; j++) |
---|
| 544 | a.push({name: n, value: v[j]}); |
---|
| 545 | } |
---|
| 546 | else if (v !== null && typeof v != 'undefined') |
---|
| 547 | a.push({name: n, value: v}); |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | if (!semantic && form.clk) { |
---|
| 551 | // input type=='image' are not found in elements array! handle them here |
---|
| 552 | var inputs = form.getElementsByTagName("input"); |
---|
| 553 | for(var i=0, max=inputs.length; i < max; i++) { |
---|
| 554 | var input = inputs[i]; |
---|
| 555 | var n = input.name; |
---|
| 556 | if(n && !input.disabled && input.type == "image" && form.clk == input) |
---|
| 557 | a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
---|
| 558 | } |
---|
| 559 | } |
---|
| 560 | return a; |
---|
| 561 | }; |
---|
| 562 | |
---|
| 563 | |
---|
| 564 | /** |
---|
| 565 | * Serializes form data into a 'submittable' string. This method will return a string |
---|
| 566 | * in the format: name1=value1&name2=value2 |
---|
| 567 | * |
---|
| 568 | * The semantic argument can be used to force form serialization in semantic order. |
---|
| 569 | * If your form must be submitted with name/value pairs in semantic order then pass |
---|
| 570 | * true for this arg, otherwise pass false (or nothing) to avoid the overhead for |
---|
| 571 | * this logic (which can be significant for very large forms). |
---|
| 572 | * |
---|
| 573 | * @example var data = $("#myForm").formSerialize(); |
---|
| 574 | * $.ajax('POST', "myscript.cgi", data); |
---|
| 575 | * @desc Collect all the data from a form into a single string |
---|
| 576 | * |
---|
| 577 | * @name formSerialize |
---|
| 578 | * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) |
---|
| 579 | * @type String |
---|
| 580 | * @cat Plugins/Form |
---|
| 581 | */ |
---|
| 582 | $.fn.formSerialize = function(semantic) { |
---|
| 583 | //hand off to jQuery.param for proper encoding |
---|
| 584 | return $.param(this.formToArray(semantic)); |
---|
| 585 | }; |
---|
| 586 | |
---|
| 587 | |
---|
| 588 | /** |
---|
| 589 | * Serializes all field elements in the jQuery object into a query string. |
---|
| 590 | * This method will return a string in the format: name1=value1&name2=value2 |
---|
| 591 | * |
---|
| 592 | * The successful argument controls whether or not serialization is limited to |
---|
| 593 | * 'successful' controls (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). |
---|
| 594 | * The default value of the successful argument is true. |
---|
| 595 | * |
---|
| 596 | * @example var data = $("input").formSerialize(); |
---|
| 597 | * @desc Collect the data from all successful input elements into a query string |
---|
| 598 | * |
---|
| 599 | * @example var data = $(":radio").formSerialize(); |
---|
| 600 | * @desc Collect the data from all successful radio input elements into a query string |
---|
| 601 | * |
---|
| 602 | * @example var data = $("#myForm :checkbox").formSerialize(); |
---|
| 603 | * @desc Collect the data from all successful checkbox input elements in myForm into a query string |
---|
| 604 | * |
---|
| 605 | * @example var data = $("#myForm :checkbox").formSerialize(false); |
---|
| 606 | * @desc Collect the data from all checkbox elements in myForm (even the unchecked ones) into a query string |
---|
| 607 | * |
---|
| 608 | * @example var data = $(":input").formSerialize(); |
---|
| 609 | * @desc Collect the data from all successful input, select, textarea and button elements into a query string |
---|
| 610 | * |
---|
| 611 | * @name fieldSerialize |
---|
| 612 | * @param successful true if only successful controls should be serialized (default is true) |
---|
| 613 | * @type String |
---|
| 614 | * @cat Plugins/Form |
---|
| 615 | */ |
---|
| 616 | $.fn.fieldSerialize = function(successful) { |
---|
| 617 | var a = []; |
---|
| 618 | this.each(function() { |
---|
| 619 | var n = this.name; |
---|
| 620 | if (!n) return; |
---|
| 621 | var v = $.fieldValue(this, successful); |
---|
| 622 | if (v && v.constructor == Array) { |
---|
| 623 | for (var i=0,max=v.length; i < max; i++) |
---|
| 624 | a.push({name: n, value: v[i]}); |
---|
| 625 | } |
---|
| 626 | else if (v !== null && typeof v != 'undefined') |
---|
| 627 | a.push({name: this.name, value: v}); |
---|
| 628 | }); |
---|
| 629 | //hand off to jQuery.param for proper encoding |
---|
| 630 | return $.param(a); |
---|
| 631 | }; |
---|
| 632 | |
---|
| 633 | |
---|
| 634 | /** |
---|
| 635 | * Returns the value(s) of the element in the matched set. For example, consider the following form: |
---|
| 636 | * |
---|
| 637 | * <form><fieldset> |
---|
| 638 | * <input name="A" type="text" /> |
---|
| 639 | * <input name="A" type="text" /> |
---|
| 640 | * <input name="B" type="checkbox" value="B1" /> |
---|
| 641 | * <input name="B" type="checkbox" value="B2"/> |
---|
| 642 | * <input name="C" type="radio" value="C1" /> |
---|
| 643 | * <input name="C" type="radio" value="C2" /> |
---|
| 644 | * </fieldset></form> |
---|
| 645 | * |
---|
| 646 | * var v = $(':text').fieldValue(); |
---|
| 647 | * // if no values are entered into the text inputs |
---|
| 648 | * v == ['',''] |
---|
| 649 | * // if values entered into the text inputs are 'foo' and 'bar' |
---|
| 650 | * v == ['foo','bar'] |
---|
| 651 | * |
---|
| 652 | * var v = $(':checkbox').fieldValue(); |
---|
| 653 | * // if neither checkbox is checked |
---|
| 654 | * v === undefined |
---|
| 655 | * // if both checkboxes are checked |
---|
| 656 | * v == ['B1', 'B2'] |
---|
| 657 | * |
---|
| 658 | * var v = $(':radio').fieldValue(); |
---|
| 659 | * // if neither radio is checked |
---|
| 660 | * v === undefined |
---|
| 661 | * // if first radio is checked |
---|
| 662 | * v == ['C1'] |
---|
| 663 | * |
---|
| 664 | * The successful argument controls whether or not the field element must be 'successful' |
---|
| 665 | * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). |
---|
| 666 | * The default value of the successful argument is true. If this value is false the value(s) |
---|
| 667 | * for each element is returned. |
---|
| 668 | * |
---|
| 669 | * Note: This method *always* returns an array. If no valid value can be determined the |
---|
| 670 | * array will be empty, otherwise it will contain one or more values. |
---|
| 671 | * |
---|
| 672 | * @example var data = $("#myPasswordElement").fieldValue(); |
---|
| 673 | * alert(data[0]); |
---|
| 674 | * @desc Alerts the current value of the myPasswordElement element |
---|
| 675 | * |
---|
| 676 | * @example var data = $("#myForm :input").fieldValue(); |
---|
| 677 | * @desc Get the value(s) of the form elements in myForm |
---|
| 678 | * |
---|
| 679 | * @example var data = $("#myForm :checkbox").fieldValue(); |
---|
| 680 | * @desc Get the value(s) for the successful checkbox element(s) in the jQuery object. |
---|
| 681 | * |
---|
| 682 | * @example var data = $("#mySingleSelect").fieldValue(); |
---|
| 683 | * @desc Get the value(s) of the select control |
---|
| 684 | * |
---|
| 685 | * @example var data = $(':text').fieldValue(); |
---|
| 686 | * @desc Get the value(s) of the text input or textarea elements |
---|
| 687 | * |
---|
| 688 | * @example var data = $("#myMultiSelect").fieldValue(); |
---|
| 689 | * @desc Get the values for the select-multiple control |
---|
| 690 | * |
---|
| 691 | * @name fieldValue |
---|
| 692 | * @param Boolean successful true if only the values for successful controls should be returned (default is true) |
---|
| 693 | * @type Array<String> |
---|
| 694 | * @cat Plugins/Form |
---|
| 695 | */ |
---|
| 696 | $.fn.fieldValue = function(successful) { |
---|
| 697 | for (var val=[], i=0, max=this.length; i < max; i++) { |
---|
| 698 | var el = this[i]; |
---|
| 699 | var v = $.fieldValue(el, successful); |
---|
| 700 | if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) |
---|
| 701 | continue; |
---|
| 702 | v.constructor == Array ? $.merge(val, v) : val.push(v); |
---|
| 703 | } |
---|
| 704 | return val; |
---|
| 705 | }; |
---|
| 706 | |
---|
| 707 | /** |
---|
| 708 | * Returns the value of the field element. |
---|
| 709 | * |
---|
| 710 | * The successful argument controls whether or not the field element must be 'successful' |
---|
| 711 | * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). |
---|
| 712 | * The default value of the successful argument is true. If the given element is not |
---|
| 713 | * successful and the successful arg is not false then the returned value will be null. |
---|
| 714 | * |
---|
| 715 | * Note: If the successful flag is true (default) but the element is not successful, the return will be null |
---|
| 716 | * Note: The value returned for a successful select-multiple element will always be an array. |
---|
| 717 | * Note: If the element has no value the return value will be undefined. |
---|
| 718 | * |
---|
| 719 | * @example var data = jQuery.fieldValue($("#myPasswordElement")[0]); |
---|
| 720 | * @desc Gets the current value of the myPasswordElement element |
---|
| 721 | * |
---|
| 722 | * @name fieldValue |
---|
| 723 | * @param Element el The DOM element for which the value will be returned |
---|
| 724 | * @param Boolean successful true if value returned must be for a successful controls (default is true) |
---|
| 725 | * @type String or Array<String> or null or undefined |
---|
| 726 | * @cat Plugins/Form |
---|
| 727 | */ |
---|
| 728 | $.fieldValue = function(el, successful) { |
---|
| 729 | var n = el.name, t = el.type, tag = el.tagName.toLowerCase(); |
---|
| 730 | if (typeof successful == 'undefined') successful = true; |
---|
| 731 | |
---|
| 732 | if (successful && (!n || el.disabled || t == 'reset' || t == 'button' || |
---|
| 733 | (t == 'checkbox' || t == 'radio') && !el.checked || |
---|
| 734 | (t == 'submit' || t == 'image') && el.form && el.form.clk != el || |
---|
| 735 | tag == 'select' && el.selectedIndex == -1)) |
---|
| 736 | return null; |
---|
| 737 | |
---|
| 738 | if (tag == 'select') { |
---|
| 739 | var index = el.selectedIndex; |
---|
| 740 | if (index < 0) return null; |
---|
| 741 | var a = [], ops = el.options; |
---|
| 742 | var one = (t == 'select-one'); |
---|
| 743 | var max = (one ? index+1 : ops.length); |
---|
| 744 | for(var i=(one ? index : 0); i < max; i++) { |
---|
| 745 | var op = ops[i]; |
---|
| 746 | if (op.selected) { |
---|
| 747 | // extra pain for IE... |
---|
| 748 | var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value; |
---|
| 749 | if (one) return v; |
---|
| 750 | a.push(v); |
---|
| 751 | } |
---|
| 752 | } |
---|
| 753 | return a; |
---|
| 754 | } |
---|
| 755 | return el.value; |
---|
| 756 | }; |
---|
| 757 | |
---|
| 758 | |
---|
| 759 | /** |
---|
| 760 | * Clears the form data. Takes the following actions on the form's input fields: |
---|
| 761 | * - input text fields will have their 'value' property set to the empty string |
---|
| 762 | * - select elements will have their 'selectedIndex' property set to -1 |
---|
| 763 | * - checkbox and radio inputs will have their 'checked' property set to false |
---|
| 764 | * - inputs of type submit, button, reset, and hidden will *not* be effected |
---|
| 765 | * - button elements will *not* be effected |
---|
| 766 | * |
---|
| 767 | * @example $('form').clearForm(); |
---|
| 768 | * @desc Clears all forms on the page. |
---|
| 769 | * |
---|
| 770 | * @name clearForm |
---|
| 771 | * @type jQuery |
---|
| 772 | * @cat Plugins/Form |
---|
| 773 | */ |
---|
| 774 | $.fn.clearForm = function() { |
---|
| 775 | return this.each(function() { |
---|
| 776 | $('input,select,textarea', this).clearFields(); |
---|
| 777 | }); |
---|
| 778 | }; |
---|
| 779 | |
---|
| 780 | /** |
---|
| 781 | * Clears the selected form elements. Takes the following actions on the matched elements: |
---|
| 782 | * - input text fields will have their 'value' property set to the empty string |
---|
| 783 | * - select elements will have their 'selectedIndex' property set to -1 |
---|
| 784 | * - checkbox and radio inputs will have their 'checked' property set to false |
---|
| 785 | * - inputs of type submit, button, reset, and hidden will *not* be effected |
---|
| 786 | * - button elements will *not* be effected |
---|
| 787 | * |
---|
| 788 | * @example $('.myInputs').clearFields(); |
---|
| 789 | * @desc Clears all inputs with class myInputs |
---|
| 790 | * |
---|
| 791 | * @name clearFields |
---|
| 792 | * @type jQuery |
---|
| 793 | * @cat Plugins/Form |
---|
| 794 | */ |
---|
| 795 | $.fn.clearFields = $.fn.clearInputs = function() { |
---|
| 796 | return this.each(function() { |
---|
| 797 | var t = this.type, tag = this.tagName.toLowerCase(); |
---|
| 798 | if (t == 'text' || t == 'password' || tag == 'textarea') |
---|
| 799 | this.value = ''; |
---|
| 800 | else if (t == 'checkbox' || t == 'radio') |
---|
| 801 | this.checked = false; |
---|
| 802 | else if (tag == 'select') |
---|
| 803 | this.selectedIndex = -1; |
---|
| 804 | }); |
---|
| 805 | }; |
---|
| 806 | |
---|
| 807 | |
---|
| 808 | /** |
---|
| 809 | * Resets the form data. Causes all form elements to be reset to their original value. |
---|
| 810 | * |
---|
| 811 | * @example $('form').resetForm(); |
---|
| 812 | * @desc Resets all forms on the page. |
---|
| 813 | * |
---|
| 814 | * @name resetForm |
---|
| 815 | * @type jQuery |
---|
| 816 | * @cat Plugins/Form |
---|
| 817 | */ |
---|
| 818 | $.fn.resetForm = function() { |
---|
| 819 | return this.each(function() { |
---|
| 820 | // guard against an input with the name of 'reset' |
---|
| 821 | // note that IE reports the reset function as an 'object' |
---|
| 822 | if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) |
---|
| 823 | this.reset(); |
---|
| 824 | }); |
---|
| 825 | }; |
---|
| 826 | |
---|
| 827 | |
---|
| 828 | /** |
---|
| 829 | * Enables or disables any matching elements. |
---|
| 830 | * |
---|
| 831 | * @example $(':radio').enabled(false); |
---|
| 832 | * @desc Disables all radio buttons |
---|
| 833 | * |
---|
| 834 | * @name select |
---|
| 835 | * @type jQuery |
---|
| 836 | * @cat Plugins/Form |
---|
| 837 | */ |
---|
| 838 | $.fn.enable = function(b) { |
---|
| 839 | if (b == undefined) b = true; |
---|
| 840 | return this.each(function() { |
---|
| 841 | this.disabled = !b |
---|
| 842 | }); |
---|
| 843 | }; |
---|
| 844 | |
---|
| 845 | /** |
---|
| 846 | * Checks/unchecks any matching checkboxes or radio buttons and |
---|
| 847 | * selects/deselects and matching option elements. |
---|
| 848 | * |
---|
| 849 | * @example $(':checkbox').selected(); |
---|
| 850 | * @desc Checks all checkboxes |
---|
| 851 | * |
---|
| 852 | * @name select |
---|
| 853 | * @type jQuery |
---|
| 854 | * @cat Plugins/Form |
---|
| 855 | */ |
---|
| 856 | $.fn.select = function(select) { |
---|
| 857 | if (select == undefined) select = true; |
---|
| 858 | return this.each(function() { |
---|
| 859 | var t = this.type; |
---|
| 860 | if (t == 'checkbox' || t == 'radio') |
---|
| 861 | this.checked = select; |
---|
| 862 | else if (this.tagName.toLowerCase() == 'option') { |
---|
| 863 | var $sel = $(this).parent('select'); |
---|
| 864 | if (select && $sel[0] && $sel[0].type == 'select-one') { |
---|
| 865 | // deselect all other options |
---|
| 866 | $sel.find('option').select(false); |
---|
| 867 | } |
---|
| 868 | this.selected = select; |
---|
| 869 | } |
---|
| 870 | }); |
---|
| 871 | }; |
---|
| 872 | |
---|
| 873 | })(jQuery); |
---|