JS to get Radio value code detailed

Source: Internet
Author: User

Put the radio input in the form,

<form name=theform>

<input type=text name=input1 value= "Test" >

<input type=radio name= "a[]" value= "0" checked> option 1
<input type=radio name= "a[]" value= "1" > option 2

</form>

Instance 1

==javascript read the value of radio = =

The code is as follows Copy Code

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 ("Radio box unchecked");
return false;
}
</script>


Instance 2

The user clicks the current radio implementation to read the current radio value

The code is as follows Copy Code

<script type= "Text/javascript" language= "JavaScript" >
function Permissions (obj)
{
var radioss= obj.value
alert (Radioss);
}
</script>
<body>
<input type= "Radio" id= "Radios" name= "radios" value= "0" onclick= "Permissions (This)" >admin
<input type= "Radio" id= "Radios" name= "Radios" value= "1" onclick= "Permissions (This)" >user


Instance 3

= = Set Radio which is selected = =

The code is as follows Copy Code

<script>

function Setradio (v)
{
Robj=document.form1.radioname;
for (i=0;i<robj.length;i++) {
if (robj[i].value==v) {
Robj[i].checked=true;
}
}
}
</script>

Edit summary:
The most important thing we see is

The code is as follows Copy Code
if (formobj.a[i].checked) {
Alert ("Select box selected: + Formobj.a[i].value");
return true;
}

FORMOJB is the name of the form we want to define, this must be the same as in JS, and then A is our multiple radio named a[] This form, a[i] refers to the radio of the first few Formobj.a[i].value The key to this is to get the value in the Formobj.a[i].value.

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.