Use Javascript to validate radio (radio) values in forms (form) effects

Source: Internet
Author: User
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.
Related Article

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.