<SCRIPT type = "text/JavaScript">
// Description: use JavaScript to verify the single-choice (radio) value in the form (Form ).
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:
The Code is as follows: var radiotest = Document. Forms ['testform']. elements ['radiotest'];
If (getradiovalue (radiotest) = '')
{......}
Perform the operations you want based on the judgment results.
Same as a single regionJavascriptWhen you verify the value of the Multi-choice box (checkbox) in the form (Form), you will encounter problems becauseCheckboxIt is very different from a common text box in obtaining values. This article introduces a more common way to obtain values.CheckboxValue method.
Javascript:
-
- <SCRIPT type = "text/JavaScript">
-
- // Description: use JavaScript to verify the value of the Multi-choice box (checkbox) in the form (Form ).
-
-
- Function getcheckboxvalue (checkbox)
- {
- If (! Checkbox. Length & checkbox. type. tolowercase () =
'Checkbox ')
- {Return
(Checkbox. Checked )? Checkbox. Value :'';
}
- If (checkbox [0]. tagname. tolowercase ()
! = 'Input' |
- Checkbox [0]. type. tolowercase ()! =
'Checkbox ')
- {Return
'';}
-
- Var val = [];
- VaR Len = checkbox. length;
- For (I = 0; I <Len; I ++)
- {
- If (checkbox [I]. Checked)
- {
- Val [Val. Length] = checkbox [I]. value;
- }
- }
- Return (Val. Length )? VAL :'';
- }
-
- </SCRIPT>
- AndRadioAllNameSame. There are multiple values.CheckboxValue, we cannot follow the common text box. ValueTo determine which one is selected.
When a groupCheckboxWhen there are multiple options, we can useCheckbox [I]To determine whether an option is selected to return the value.CheckboxWhen there is only one option, the way to get the value changes.(Checkbox. Checked )? Checkbox. Value :'';And then return the corresponding value.
The parameters passed in the above Code areCheckboxObjects, such:
VaRCheckboxtest=Document. Forms ['testform']. elements ['checkboxtest'];
If(Getcheckboxvalue (checkboxtest)='')
{......}
Perform the operations you want based on the judgment results.