The JQuery Val () method source code interpretation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.