JS gets the value of radio

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax button document function get group html http

In fact, the value of JS to get Radio is very simple, if it is a radio button group to traverse the line, if the radio is directly judged. Value on the line.

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-type" content= "HTML; charset=gb2312 "/>
<TITLE>JS Get Radio Value </title>
<script>
function Radiogroup ()
{

        for (var i=0;i<formobj.a.length;i++) {  
            if (formobj.a[i].checked) {
              Alert ("Select box selected:" + formobj.a[i].value);
             return true;
           }
       }
        alert ("Unchecked");
        return false;
       }
</script>
Instance two

<script>
function Search (obj)
{
var Urlparam
For (I=0;i<obj.getelementsbytagname ("input"). length-1;i++)
{
if (Obj.getelementsbytagname ("input") [i].checked)
{
Urlparam=obj.getelementsbytagname ("input") [I].value
Break
}
}
obj.action+= "&sjprice=" +urlparam
return True
}
</script>

</head>

<body>
<form id= "form" name= "form" method= "POST" action= ""
  <p>
     <LABEL>
      <input type= "Radio" name= "radiogroup1[" "value=" 1 "id=" Radiogroup1_0 "/>
      Radio </label>
    <br/>
    <label>
      <input type= "Radio" name= "radiogroup1[" Value= "1" id= "Radiogroup1_1"/>
      Radio </label>
    <br />
    <label>
      <input type= Radio "Name=" Radiogroup1[] "value=" 1 "id=" radiogroup1_2 "/>
      Radio </label>
     <br/>
    <label>
      <input type= " Radio "Name=" radiogroup1[] "value=" 1 "id=" radiogroup1_3 "/>
    &nbsP; Radio </label>
  </P>
  <p>
    <input type= button "Name=" Button "id=" button "value=" detect "  onclick=" Web effects: Radiogroup ();/>
    <br/>
  </p>
</form>
</body>
</html>
Method Three

A section of JS get radio a set of selected values of the code, issued to share with you!

Preview function
var colorid= "";
function Showtemp ()

{
var eless = document.getelementsbyname ("Colorstyle");
for (Var i=0;i<eless.length;i++)

{
alert (eless[i].checked+eless[i].name+eless[i].id);
if (eless[i].checked)

{
Colorid=eless[i].value;
Break;
}
}
Alert (colorid+ "-");
}
Method Four, a complete example

<script lanage= "JavaScript" >
function Isid ()
{
alert (Document.all.gender.value);
if (document.all.gender.value= ' new ')
{
Eval ("document.all.emp.style.display= ' block ')"
return false;
}else
{
Eval ("document.all.emp.style.display= ' None")
return true;
}
}

Function foo ()
{
    var selectedindex =-1;
    var Form1 = document.getElementById ("Form1");
    var i = 0;
   
    for (i=0 i<form1.gender.length; i++)
    {
        if (form1.gender[i].checked)
         {
            selectedindex = i;
             alert ("The value of your selected item is:" + form1.gender[i].value);
            break;
       }
   }
   
    if (SelectedIndex < 0)
    {
         alert ("You have not selected any items");
   }
}
</script>
<bodY>
<form method= "POST"
  <table>
  <tr><td>
  <input type = "Radio" value= "new"/>
    employee
    <input type= "Radio" value= "old" checked = "Checked"/>
    old employee
  <input type= "text"/>
  </td>
  < /tr>
  </table>
</form>

</body>
</html>

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.