About determining whether a radio box is checked for detailed instructions

Source: Internet
Author: User
Tags hash

About determining whether a radio box is checked for detailed instructions

It's a core piece of code.

for (Var i=0,l=radios.length;i<l;i++)
{
if (!hash[radios[i].name])
Hash[radios[i].name] = [Radios[i]]
Else
Hash[radios[i].name].push (Radios[i]);
}


Now let's take a look at the detailed application examples

<body>
<input type= "Radio" Name= "a" checked=checked value= "addd"/><input type= "Radio" Name= "a" value= "ACCC"/> <input type= "Radio" Name= "a" value= "ACCC"/><input type= "Radio" Name= "a" value= "ACCC"/>
<input type= "Radio" name= "B" value= "BCCC"/><input type= "Radio" name= "B" value= "BCCC"/><input type= " Radio "Name=" "B" value= "BCCC"/>
<input type= "Radio" name= "C" checked=checked value= "ceee"/><input type= "Radio" name= "C" value= "Ceee"/> <input type= "Radio" name= "C" value= "Ceee"/>
<input type= "Radio" name= "D" checked=checked value= "DFFF"/><input type= "Radio" name= "D" value= "Dfff"/> <input type= "Radio" name= "D" value= "Dfff"/>
<input type= "Radio" Name= "E" checked=checked value= "ehhh"/><input type= "Radio" Name= "E" value= "Ehhh"/> <input type= "Radio" Name= "E" value= "Ehhh"/>
</body>
<script>
function Check () {
var mm=document.getelementsbytagname ("input");
var mask=0;
var temp=[];
var temp1=[];
if (mm.lenght<1) {alert (' err ');}
else{
for (i=0;i<mm.length;i++) {
if (mm[i].type== "Radio" &&mm[i].checked==true) {
Temp.push (Mm[i].value)
mask++;
}
else if (mm[i].type== "Radio" &&mm[i].checked==false) {
Temp1.push (Mm[i].value)

}
}
Alert ("Selected" +temp+ "N is not selected" +TEMP1);
if (mask==0) {alert (' no checked ')}
}
}
Check ();
</script>

Change automatic selection to single selection

<lable>
<input type= "Radio" name= "a"/> The <input type= "Radio" name= "a"/> <input type= "Radio" name= "a"/> <inpu T type= "Radio" name= "a"/>
</lable>
<br/>
<lable>
<input type= "Radio" name= "B"/> <input "type=" Radio "" B "name=/> <input" type= "Radio" B "name=/> T type= "Radio" name= "B"/>
</lable>
<br/>
<lable>
<input type= "Radio" name= "C"/> <input "type= Radio" name= "C"/> <input type= "Radio" name= "C"/> <inpu T type= "Radio" name= "C"/>
</lable>
<div><button onclick= "Checkradio ()" >checkall</button></div>
<script type= "text/web Effects" >
function Checkradio ()
{
var inputs = document.getElementsByTagName ("input");
var radios = [];
for (Var i=0,l=inputs.length;i<l;i++)
{
if (inputs[i].type== "Radio" && Inputs[i].name)
Radios.push (Inputs[i]);
}
var hash= {};
for (Var i=0,l=radios.length;i<l;i++)
{
if (!hash[radios[i].name])
Hash[radios[i].name] = [Radios[i]]
Else
Hash[radios[i].name].push (Radios[i]);
}
for (var i in hash)
{
var iflag = false;
for (Var j=0,l=hash[i].length;j<l;j++)
{
if (hash[i][j].checked)
{
Iflag = true;
Break
}
}
if (!iflag)
{
Alert ("Name +i+") has not been selected for this set of radio boxes! ");
return false;
}
}
Alert ("Congratulations,all pass!");
return true;
}
</script>

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.