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: