Valfunction(value) {varhooks, ret, isfunction, Elem= This[0]; if(!arguments.length) {//No Parameters if(Elem) {//first element //Consider the case where the element is checkbox,radio,option or select, when there is a Val hookHooks = jquery.valhooks[Elem.type] | |jquery.valhooks[elem.nodeName.toLowerCase ()]; //If the hook is present and has a get and the value obtained is not undefined, the value is returned if(Hooks && "get"inchHooks && (ret = Hooks.get (Elem, "value"))!==undefined) { returnret; } //Otherwise, RET is the value of the elementRET =Elem.value; //If the value is a string return typeofret = = = "string"?//replace the line break with ""Ret.replace (Rreturn, "") : //If the value is not a string, consider the case of null and undefined, if NULL or Undefined,ret is set to ""RET = =NULL? "": ret; } return; } //determines whether the passed in parameter value is a functionIsfunction =jquery.isfunction (value); //traversing set values return This. each (function(i) {varVal; if( This. NodeType!== 1) {//if it is not an element node, return return; } if(isfunction) {//if the parameter of a function function is the first index, the second is the corresponding valueval = Value.call ( This, I, JQuery ( This). Val ()); } Else{val=value; } //if Val is null or undefined, set to "" if(val = =NULL) {Val= ""; //If Val is a number, convert to a string}Else if(typeofval = = = "Number") {Val+= ""; //If Val is an array}Else if(Jquery.isarray (Val)) {//working with undefined null and numeric in an arrayval = Jquery.map (Val,function(value) {returnValue = =NULL? "": Value + ""; }); } //Similarly, consider the case where the element is checkbox,radio,option or selectHooks = jquery.valhooks[ This. Type] | | jquery.valhooks[ This. Nodename.tolowercase ()]; //If set returns undefined, fall back to normal Val if(!hooks | |! ("Set"inchHooks) | | Hooks.set ( This, Val, "value") = = =undefined) { This. Value =Val; } }); }
Valhooks to be continue
The JQuery Val () method source code interpretation