JS how to get the value of the selected radio radio button

Source: Internet
Author: User

JS how to get the value of the selected radio radio button:
Radio radio Button is one of the most common form elements, often need to get the value of the selected button, the following is an example of how to use JavaScript to implement this function, code examples are as follows:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ant Tribe</title><Scripttype= "Text/javascript">window.onload=function(){  varObox=document.getElementById ("Box"); Radios=Obox.getelementsbytagname ("input"); varBT=document.getElementById ("BT"); Bt.onclick=function()  {     for(varI=0; I<Radios.length;i++)    {      if(radios[i].checked==true) {alert (radios[i].value); }    }  }}</Script></Head><Body><DivID= "box">  <inputtype= "Radio"name= "RA"value= "1"/>  <inputtype= "Radio"name= "RA"value= "2"/>  <inputtype= "Radio"name= "RA"value= "3"/>  <inputtype= "Radio"name= "RA"value= "4"/>  <inputtype= "Radio"name= "RA"value= "5"/></Div><inputtype= "button"ID= "BT"value= "Click to view Results" /></Body></HTML>

The above code when clicking on the button can pop up the selected radio button Value property value, the code is very simple the following is a brief introduction to the implementation process.
I. Principle of implementation:
Use the getElementsByTagName () function to get the INPUT element object under the Div, which is the radio button in this code, and then iterate through each button with a for loop, and determine if the value of the current radio button's Checked property is true. If Tue is selected, then the Value property values are popped, which is generally true.
two. Related reading:
1.window.onload can be found in the window.onload usage section.
the 2.getElementById () function can be found in the document.getElementById () method section of JavaScript .
the 3.getElementsByTagName () function can be found in the document.getElementsByTagName () section of JavaScript .
4.onclick events can be found in the OnClick events section of JavaScript .
5.checked properties can be found in the Radio.checked Properties section of JavaScript .

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9693

For more information, refer to: http://www.softwhy.com/javascript/

JS how to get the value of the selected radio radio button

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.