Get the value of radio through the event processing of JS

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_50a1e17401017pik.html

Advance Knowledge Preparation:

In an HTML document, each element can set the ID and name attribute.

Where the id attribute is a unique attribute and cannot be duplicated, an ID value can only correspond to one element;

The Name property can be repeated, and a name value can correspond to a set of elements.
Therefore, only one selected HTML element is returned when using the document.getelementbyidx_x (ID);
When you use Document.getelementsbyname (NAME), you return an array of multiple HTML elements

(an array is returned even if there is only one element in the HTML page that meets the requirements.)

Document.getelementsbyname (NAME) is used to select a set of CheckBox or radio in form forms

Method 1:

<script type = "Text/javascript" >
function Change ()
{
var radio =document.getelementsbyname ("Form1");
var radio =document.getelementbyidx_x ("Form1");

You can't get all the radio values with Byid, but each return is 1.
Varradiolength = Radio.length;
for (var i =0;i < radiolength;i++)
{
if (radio[i].checked)
{
Varradiovalue = Radio[i].value;
alert (Radiovalue);
}
}
}

</script>

<body>
<input type = "Radio" id = "Form1" name = "Form1" value = "1" onchange = "Change ();" > Option 1
<input type = "Radio" id = "Form1" name = "Form1" value = "2" onchange = "Change ();" > Option 2

</body>

Method 2:

    <script type = "Text/javascript"
   function change ()
  {
   varnew=document.getelementsbyname ("Form1");
    varstrNew;
   for (vari=0;i<new.length;i++)
   {
    if ( New.item (i). Checked)  

{
Strnew=new.item (i). getattribute ("value");
alert (strnew); Item () Method: Returns the current item in the collection
Break
}
Else
{
Continue
}
}
}
</script>

<body>
<input type = "Radio" id = "Form1" name = "Form1" value = "1" onchange = "Change ();" > Option 1
<input type = "Radio" id = "Form1" name = "Form1" value = "2" onchange = "Change ();" > Option 2
</body>

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.