Javascript code for getting server control values _ javascript skills

Source: Internet
Author: User
Js Code for obtaining server control values 1. Obtain the value and text of the selected items in the drop-down list (select)
Sample Code select.htm:

The Code is as follows:



Obtain the value and text of the selected item in the drop-down list (select)

Script
// Obtain the text of the selected item in the drop-down list
Function getSelectedText (name ){
Var obj = document. getElementById (name );
For (I = 0; I If (obj [I]. selected = true ){
Return obj [I]. innerText; // the key is to obtain the option text through the innerText attribute of the option object.
}
}
}
// Obtain the value of the selected item in the drop-down list
Function getSelectedValue (name ){
Var obj = document. getElementById (name );
Return obj. value; // This is simple and can be obtained directly using the value Attribute of the object.
}
Script
1 2 3





2. Obtain the value of the radio button group and modify the selected items
I have seen many posts saying that js can directly use document to obtain the value of a single-choice button (radio) group. getElementById ("delimiter "). value, although the drop-down list (also an array of list items) with the same as the button group selected for a single item is also an array, the value of the drop-down list can be obtained in this way, however, the selected value is not displayed in the single-choice button group. After careful research, the summary is as follows:
Unlike the drop-down list, the single-choice button must use this. form. optional or document. getElementsByName ('authorization') method to obtain the array object, document. getElementById ('ignore') cannot obtain this array object (select can ). In addition, to obtain the value, it must be obtained through a loop judgment. value (select can) cannot be used directly ). To change the selected items of a Single-choice button group, you must use a loop to determine and change the value of each single-choice button.
The test code radio.html is as follows:

The Code is as follows:




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.