This paper introduces a more general method of obtaining radio value, and hopes to be useful to novice.
Copy Code code as follows:
<script type= "Text/javascript" >
Note: Use Javascript to verify Radio (radio) values in forms (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>
Radio is the same name as the checkbox, and there are multiple values, and when we get the radio value, we can't follow the normal text box. Value, but to determine which is selected.
When a group of radio has multiple options, we can use loops to radio[i to determine whether an option is selected to return a value, but when a group of radio has only one option, the way to get the value changes, and the code returns (radio.checked)? Radio.value: '; This way you directly determine if it is selected, and then return the corresponding value.
The code passed in above is the radio object, such as:
Copy Code code as follows:
var radiotest = document.forms[' testform '].elements[' radiotest '];
if (Getradiovalue (radiotest) = = ")
{ ...... }
Perform the actions you want based on the results of your judgment.