📄 jquery.form.js
字号:
// take a breath so that pending repaints get some cpu time before the upload starts setTimeout(function() { // make sure form attrs are set var encAttr = form.encoding ? 'encoding' : 'enctype'; var t = $form.attr('target'); $form.attr({ target: id, method: 'POST', action: opts.url }); form[encAttr] = 'multipart/form-data'; // support timout if (opts.timeout) setTimeout(function() { timedOut = true; cb(); }, opts.timeout); // add iframe to doc and submit the form $io.appendTo('body'); io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false); form.submit(); $form.attr('target', t); // reset target }, 10); function cb() { if (cbInvoked++) return; io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false); var ok = true; try { if (timedOut) throw 'timeout'; // extract the server response from the iframe var data, doc; doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document; xhr.responseText = doc.body ? doc.body.innerHTML : null; xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc; if (opts.dataType == 'json' || opts.dataType == 'script') { var ta = doc.getElementsByTagName('textarea')[0]; data = ta ? ta.value : xhr.responseText; if (opts.dataType == 'json') eval("data = " + data); else $.globalEval(data); } else if (opts.dataType == 'xml') { data = xhr.responseXML; if (!data && xhr.responseText != null) data = toXml(xhr.responseText); } else { data = xhr.responseText; } } catch(e){ ok = false; $.handleError(opts, xhr, 'error', e); } // ordering of these callbacks/triggers is odd, but that's how $.ajax does it if (ok) { opts.success(data, 'success'); if (g) $.event.trigger("ajaxSuccess", [xhr, opts]); } if (g) $.event.trigger("ajaxComplete", [xhr, opts]); if (g && ! --$.active) $.event.trigger("ajaxStop"); if (opts.complete) opts.complete(xhr, ok ? 'success' : 'error'); // clean up setTimeout(function() { $io.remove(); xhr.responseXML = null; }, 100); }; function toXml(s, doc) { if (window.ActiveXObject) { doc = new ActiveXObject('Microsoft.XMLDOM'); doc.async = 'false'; doc.loadXML(s); } else doc = (new DOMParser()).parseFromString(s, 'text/xml'); return (doc && doc.documentElement && doc.documentElement.tagName != 'parsererror') ? doc : null; }; };};$.fn.ajaxSubmit.counter = 0; // used to create unique iframe ids/** * ajaxForm() provides a mechanism for fully automating form submission. * * The advantages of using this method instead of ajaxSubmit() are: * * 1: This method will include coordinates for <input type="image" /> elements (if the element * is used to submit the form). * 2. This method will include the submit element's name/value data (for the element that was * used to submit the form). * 3. This method binds the submit() method to the form for you. * * Note that for accurate x/y coordinates of image submit elements in all browsers * you need to also use the "dimensions" plugin (this method will auto-detect its presence). * * The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely * passes the options argument along after properly binding events for submit elements and * the form itself. See ajaxSubmit for a full description of the options argument. * * * @example * var options = { * target: '#myTargetDiv' * }; * $('#myForm').ajaxSForm(options); * @desc Bind form's submit event so that 'myTargetDiv' is updated with the server response * when the form is submitted. * * * @example * var options = { * success: function(responseText) { * alert(responseText); * } * }; * $('#myForm').ajaxSubmit(options); * @desc Bind form's submit event so that server response is alerted after the form is submitted. * * * @example * var options = { * beforeSubmit: function(formArray, jqForm) { * if (formArray.length == 0) { * alert('Please enter data.'); * return false; * } * } * }; * $('#myForm').ajaxSubmit(options); * @desc Bind form's submit event so that pre-submit callback is invoked before the form * is submitted. * * * @name ajaxForm * @param options object literal containing options which control the form submission process * @return jQuery * @cat Plugins/Form * @type jQuery */$.fn.ajaxForm = function(options) { return this.ajaxFormUnbind().submit(submitHandler).each(function() { // store options in hash this.formPluginId = $.fn.ajaxForm.counter++; $.fn.ajaxForm.optionHash[this.formPluginId] = options; $(":submit,input:image", this).click(clickHandler); });};$.fn.ajaxForm.counter = 1;$.fn.ajaxForm.optionHash = {};function clickHandler(e) { var $form = this.form; $form.clk = this; if (this.type == 'image') { if (e.offsetX != undefined) { $form.clk_x = e.offsetX; $form.clk_y = e.offsetY; } else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin var offset = $(this).offset(); $form.clk_x = e.pageX - offset.left; $form.clk_y = e.pageY - offset.top; } else { $form.clk_x = e.pageX - this.offsetLeft; $form.clk_y = e.pageY - this.offsetTop; } } // clear form vars setTimeout(function() { $form.clk = $form.clk_x = $form.clk_y = null; }, 10);};function submitHandler() { // retrieve options from hash var id = this.formPluginId; var options = $.fn.ajaxForm.optionHash[id]; $(this).ajaxSubmit(options); return false;};/** * ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm * * @name ajaxFormUnbind * @return jQuery * @cat Plugins/Form * @type jQuery */$.fn.ajaxFormUnbind = function() { this.unbind('submit', submitHandler); return this.each(function() { $(":submit,input:image", this).unbind('click', clickHandler); });};/** * formToArray() gathers form element data into an array of objects that can * be passed to any of the following ajax functions: $.get, $.post, or load. * Each object in the array has both a 'name' and 'value' property. An example of * an array for a simple login form might be: * * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] * * It is this array that is passed to pre-submit callback functions provided to the * ajaxSubmit() and ajaxForm() methods. * * The semantic argument can be used to force form serialization in semantic order. * This is normally true anyway, unless the form contains input elements of type='image'. * If your form must be submitted with name/value pairs in semantic order and your form * contains an input of type='image" then pass true for this arg, otherwise pass false * (or nothing) to avoid the overhead for this logic. * * @example var data = $("#myForm").formToArray(); * $.post( "myscript.cgi", data ); * @desc Collect all the data from a form and submit it to the server. * * @name formToArray * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) * @type Array<Object> * @cat Plugins/Form */$.fn.formToArray = function(semantic) { var a = []; if (this.length == 0) return a; var form = this[0]; var els = semantic ? form.getElementsByTagName('*') : form.elements; if (!els) return a; for(var i=0, max=els.length; i < max; i++) { var el = els[i]; var n = el.name; if (!n) continue; if (semantic && form.clk && el.type == "image") { // handle image inputs on the fly when semantic == true if(!el.disabled && form.clk == el) a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); continue; } var v = $.fieldValue(el, true); if (v && v.constructor == Array) { for(var j=0, jmax=v.length; j < jmax; j++) a.push({name: n, value: v[j]}); } else if (v !== null && typeof v != 'undefined') a.push({name: n, value: v}); } 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}); } } return a;};/** * Serializes form data into a 'submittable' string. This method will return a string * in the format: name1=value1&name2=value2 * * The semantic argument can be used to force form serialization in semantic order. * If your form must be submitted with name/value pairs in semantic order then pass * true for this arg, otherwise pass false (or nothing) to avoid the overhead for * this logic (which can be significant for very large forms). * * @example var data = $("#myForm").formSerialize(); * $.ajax('POST', "myscript.cgi", data); * @desc Collect all the data from a form into a single string * * @name formSerialize * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) * @type String * @cat Plugins/Form */$.fn.formSerialize = function(semantic) { //hand off to jQuery.param for proper encoding
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -