This article describes a more common method to obtain the radio value.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Description: use Javascript to verify the single-choice (radio) value in the form (form ).
// Author: CodeBit
Function getRadioValue (radio)
{
If (! Radio. length & radio. type. toLowerCase () = 'Radio ')
{Return (radio. checked )? Radio. value :'';}
If (radio [0]. tagName. toLowerCase ()! = 'Input' |
Radio [0]. type. toLowerCase ()! = 'Radio ')
{Return '';}
Var len = radio. length;
For (I = 0; I <len; I ++)
{
If (radio [I]. checked)
{
Return radio [I]. value;
}
}
Return '';
}
</Script>
Like the checkbox, radio has the same name and multiple values. When obtaining the radio value, we cannot follow the common text box. value method, but determine which one is selected.
When a set of radio has multiple options, we can use a loop to determine whether an option is selected to return the value in the form of radio [I, however, when a set of radio has only one option, the way to get the value changes. In the code, return (radio. checked )? Radio. value: ''; in this way, you can directly determine whether to select and then return the corresponding value.
The parameters passed in the above Code are radio objects, such:
Copy codeThe Code is as follows:
Var radioTest = document. forms ['testform']. elements ['radiotest'];
If (getRadioValue (radioTest) = '')
{......}
Perform the operations you want based on the judgment results.