📄 jquery-plugins.js
字号:
/** * 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 * @see ajaxForm * @see ajaxSubmit * @author jQuery Community */jQuery.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 = jQuery.fieldValue(el, true); if (v === null) continue; if (v.constructor == Array) { for(var j=0, jmax=v.length; j < jmax; j++) a.push({name: n, value: v[j]}); } else 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 * @see formToArray * @author jQuery Community */jQuery.fn.formSerialize = function(semantic) { //hand off to jQuery.param for proper encoding return jQuery.param(this.formToArray(semantic));};/** * Serializes all field elements in the jQuery object into a query string. * This method will return a string in the format: name1=value1&name2=value2 * * The successful argument controls whether or not serialization is limited to * 'successful' controls (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). * The default value of the successful argument is true. * * @example var data = $("input").formSerialize(); * @desc Collect the data from all successful input elements into a query string * * @example var data = $(":radio").formSerialize(); * @desc Collect the data from all successful radio input elements into a query string * * @example var data = $("#myForm :checkbox").formSerialize(); * @desc Collect the data from all successful checkbox input elements in myForm into a query string * * @example var data = $("#myForm :checkbox").formSerialize(false); * @desc Collect the data from all checkbox elements in myForm (even the unchecked ones) into a query string * * @example var data = $(":input").formSerialize(); * @desc Collect the data from all successful input, select, textarea and button elements into a query string * * @name fieldSerialize * @param successful true if only successful controls should be serialized (default is true) * @type String * @cat Plugins/Form */jQuery.fn.fieldSerialize = function(successful) { var a = []; this.each(function() { var n = this.name; if (!n) return; var v = jQuery.fieldValue(this, successful); if (v && v.constructor == Array) { for (var i=0,max=v.length; i < max; i++) a.push({name: n, value: v[i]}); } else if (v !== null && typeof v != 'undefined') a.push({name: this.name, value: v}); }); //hand off to jQuery.param for proper encoding return jQuery.param(a);};/** * Returns the value of the field element in the jQuery object. If there is more than one field element * in the jQuery object the value of the first successful one is returned. * * The successful argument controls whether or not the field element must be 'successful' * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). * The default value of the successful argument is true. If this value is false then * the value of the first field element in the jQuery object is returned. * * Note: If no valid value can be determined the return value will be undifined. * * Note: The fieldValue returned for a select-multiple element or for a checkbox input will * always be an array if it is not undefined. * * * @example var data = $("#myPasswordElement").formValue(); * @desc Gets the current value of the myPasswordElement element * * @example var data = $("#myForm :input").formValue(); * @desc Get the value of the first successful control in the jQuery object. * * @example var data = $("#myForm :checkbox").formValue(); * @desc Get the array of values for the first set of successful checkbox controls in the jQuery object. * * @example var data = $("#mySingleSelect").formValue(); * @desc Get the value of the select control * * @example var data = $("#myMultiSelect").formValue(); * @desc Get the array of selected values for the select-multiple control * * @name fieldValue * @param Boolean successful true if value returned must be for a successful controls (default is true) * @type String or Array<String> * @cat Plugins/Form */jQuery.fn.fieldValue = function(successful) { var cbVal, cbName; // loop until we find a value for (var i=0, max=this.length; i < max; i++) { var el = this[i]; var v = jQuery.fieldValue(el, successful); if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) continue; // for checkboxes, consider multiple elements, for everything else just return first valid value if (el.type != 'checkbox') return v; cbName = cbName || el.name; if (cbName != el.name) // return if we hit a checkbox with a different name return cbVal; cbVal = cbVal || []; cbVal.push(v); } return cbVal;};/** * Returns the value of the field element. * * The successful argument controls whether or not the field element must be 'successful' * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). * The default value of the successful argument is true. If the given element is not * successful and the successful arg is not false then the returned value will be null. * * Note: The fieldValue returned for a select-multiple element will always be an array. * * @example var data = jQuery.fieldValue($("#myPasswordElement")[0]); * @desc Gets the current value of the myPasswordElement element * * @name fieldValue * @param Element el The DOM element for which the value will be returned * @param Boolean successful true if value returned must be for a successful controls (default is true) * @type String or Array<String> * @cat Plugins/Form */jQuery.fieldValue = function(el, successful) { var n = el.name, t = el.type, tag = el.tagName.toLowerCase(); if (typeof successful == 'undefined') successful = true; if (successful && ( !n || el.disabled || t == 'reset' || (t == 'checkbox' || t == 'radio') && !el.checked || (t == 'submit' || t == 'image') && el.form && el.form.clk != el || tag == 'select' && el.selectedIndex == -1)) return null; if (tag == 'select') { var index = el.selectedIndex; if (index < 0) return null; var a = [], ops = el.options; var one = (t == 'select-one'); var max = (one ? index+1 : ops.length); for(var i=(one ? index : 0); i < max; i++) { var op = ops[i]; if (op.selected) { // extra pain for IE... var v = jQuery.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value; if (one) return v; a.push(v); } } return a; } return el.value;};/** * Clears the form data. Takes the following actions on the form's input fields: * - input text fields will have their 'value' property set to the empty string * - select elements will have their 'selectedIndex' property set to -1 * - checkbox and radio inputs will have their 'checked' property set to false * - inputs of type submit, button, reset, and hidden will *not* be effected * - button elements will *not* be effected * * @example $('form').clearForm(); * @desc Clears all forms on the page. * * @name clearForm * @type jQuery * @cat Plugins/Form * @see resetForm */jQuery.fn.clearForm = function() { return this.each(function() { jQuery('input,select,textarea', this).clearFields(); });};/** * Clears the selected form elements. Takes the following actions on the matched elements: * - input text fields will have their 'value' property set to the empty string * - select elements will have their 'selectedIndex' property set to -1 * - checkbox and radio inputs will have their 'checked' property set to false * - inputs of type submit, button, reset, and hidden will *not* be effected * - button elements will *not* be effected * * @example $('.myInputs').clearFields(); * @desc Clears all inputs with class myInputs * * @name clearFields * @type jQuery * @cat Plugins/Form * @see clearForm */jQuery.fn.clearFields = jQuery.fn.clearInputs = function() { return this.each(function() { var t = this.type, tag = this.tagName.toLowerCase(); if (t == 'text' || t == 'password' || tag == 'textarea') this.value = ''; else if (t == 'checkbox' || t == 'radio') this.checked = false; else if (tag == 'select') this.selectedIndex = -1; });};/** * Resets the form data. Causes all form elements to be reset to their original value. * * @example $('form').resetForm(); * @desc Resets all forms on the page. * * @name resetForm * @type jQuery * @cat Plugins/Form * @see clearForm */jQuery.fn.resetForm = function() { return this.each(function() { // guard against an input with the name of 'reset' // note that IE reports the reset function as an 'object' if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) this.reset(); });};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -