jquery Gets the selected item of a set of elements-functions, jquery Gets the check box value, jquery Gets the radio button value

Source: Internet
Author: User

When making a form submission, if you are still submitting with form, the user experience is poor, so you generally use AJAX submissions.

Where you need to get the value of each form INPUT element and get it like a text box these are okay to say, jquery provides the. Val () method, which is easy to get, but getting the check box and radio button values is cumbersome.

There's nothing to do today. Encapsulates a function, the subsequent acquisition of the check box and radio button values only need to call this function, incoming jquery selector is OK, the following is attached to the function.

Function

/** * Get selected items for a single or set of input elements * incoming jquery selector * Support: text, return: literal * support: Hidden, return: Text * Support: password, return: Text * Support: TEXTAREA, return: Text * Support: Selec T, return: 1 * Support: checkbox, return: 1,2,3,4,5,6 * Support: Radio, return: 1 * 20161203*/functionGet_choose (choose) {varChoose_size =$ (choose). Size (); varChoose_type = $ (choose+ ': eq (0) '). attr (' type ')); varReturn_text = ' ';  for(vari=0;i<choose_size;i++){        //If a check box or radio button requires special handling        if(choose_type== ' checkbox ' | | choose_type== ' Radio '){            //determines whether the element is selected and is selected for processing            if($ (choose+ ': eq (' +i+ ') '). Is (': Checked '))){                //If the check box is selected, get its value, continue to execute to get the next                if(choose_type== ' checkbox ') {Return_text+ = (return_text!= ')? ', ' +$ (choose+ ': eq (' +i+ ') '). Val (): $ (choose+ ': eq (' +i+ ') '). Val (); }                //If the radio button is selected, gets its value, terminates the loop                Else if(choose_type== ' Radio ') {Return_text+ = $ (choose+ ': eq (' +i+ ') '). Val ();  Break; }                Else{                     Break; }            }        }        //other element attributes, returning their value directly, terminating the loop        Else{Return_text+ = $ (choose+ ': eq (0) '). Val ();  Break; }    }    returnReturn_text;}

Invocation mode

Alert (Get_choose (' [name= ' text box name name "]"),alert (get_choose (' [name= ' Text field name "]"); alert (Get_choose ( ' [name= ' multiple marquee name '] '); alert (Get_choose (' [name= ') (the name of the "Radio Box"]);

jquery Gets the selected item of a set of elements-functions, jquery Gets the check box value, jquery Gets the radio button value

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.