SoFunction
Updated on 2025-04-10

"Events" caused by single selection in HTML-CSS group

Because of a radio button problem of Death, many people came out to talk nonsense. Alas, this rookie also came out and went to take advantage of the situation. I accidentally touched one.

First, let’s look at a piece of code dug out from baidu, and I will start from here.
Copy the codeThe code is as follows:

<script> 
function checkradio() 

    for(i=0;i<document.;i++) 
    { 
        if(document.[i].checked) 
        { 
alert("Your favorite fruit is:"+document.[i].);
        } 
    } 

</script> 
<form name="form1"> 
Your favorite fruit is:<br>
<input type=radio value="Fruit1" name="Fruit" checked>Apple
<input type=radio value="Fruit2" name="Fruit">Banana
<input type=radio value="Fruit3" name="Fruit">Strawberry
<input type=radio value="Fruit4" name="Fruit">Pineapple
<input type=button value="Select" onclick="checkradio()">
</form> 

The purpose of this code is to determine the selected value.

The effect you want now is to give a prompt if each option is empty, and the following code is the effect after some changes.

Program code
<script>
function checkradio()
{
    var j=0;
    for(i=0;i<document.;i++)
    {
        if(document.[i].checked==true)
        {
alert("You selected"+document.[i].);
//break;This break is invalid after Feifei's guidance, and it is removed
        }else{
            j=j+1;
            if(j==4){
alert("Damn, you choose one, I don't have to come out!");
            }
        }
    }
}
</script>
<form name="form1">
Your favorite fruit is:<br>
<input type=radio value="Fruit1" name="Fruit">
apple
<input type=radio value="Fruit2" name="Fruit">Banana
<input type=radio value="Fruit3" name="Fruit">Strawberry
<input type=radio value="Fruit4" name="Fruit">Pineapple
<input type=button value="Select" onclick="checkradio()">
</form> 


Later I saw a clearer piece of code written by Chen, so I will post it here too.
(Note: For the sake of ease of testing, I changed it, and the idea is still his idea)
Copy the codeThe code is as follows:

<script> 
function checkradio() 

    var flag=false; 
    for(var i=0;i<=document.-1;i++) 
    { 
          if([i].checked){ 
            flag=true;} 
    } 
    if(flag) 
    { 
          alert("^_^"); 
          return false; 
    }else{ 
alert("Hero, please choose one!");
    } 

</script> 


The single choice was over. Boss Feifei didn't give up and came up with a code for the check box at night.
Copy the codeThe code is as follows:

<script> 
    var j=("Fruit"); 
function allche(){ 
    for(var i=0; i <; i++){ 
            if(document.[i].checked!=true) document.[i].checked= document.; 
            if(document.[i].checked==true) document.[i].checked= document.; 
    } 

function checkall(){ 
var aa=0 
    for(var i=0; i <; i++){     
            if(document.[i].checked == true)  aa++; 
             aa!= ? document.=false : document.=true; 
    } 

function checkradio(){ 
var a=0 
var list="" 
for(var i=0; i<;i++) 
if (document.[i].checked== true){ 
list=="" ? list=document.[i].value : list=list+","+document.[i].value; 

if (list!="") alert("The fruit you like is"+list);

else{ 
a++; 
if (a==)alert("Brother. If you don't choose, how can I know what you like?");}


</script> 
<form name="form1" > 
Your favorite fruit is:<br>
<input type=checkbox value="Apple" name="Fruit" onClick="checkall()">
apple
<input type=checkbox value="banana" name="Fruit" onClick="checkall()">
banana
<input type=checkbox value="Strawberry" name="Fruit" onClick="checkall()">
strawberry
<input type=checkbox value="pineapple" name="Fruit" onClick="checkall()">
Pineapple
<input type=button value="Select" onclick="checkradio()">
<input type=checkbox   onclick="allche()" name="suoy">Select all
</form> 

<script language="javascript" type="text/javascript" > 
function test() 

    fruitlist = ""; 
    for(i=0;i<("frm").length;i++) 
        if(("frm")[i].type=="checkbox" && ("frm")[i].checked) 
            fruitlist += ("frm")[i].value + " "; 
    if(fruitlist!="") 
alert("Your favorite fruit is"+fruitlist);
    else 
alert("Brother. How do I know what you like if you don't choose?");

</script>  

Let’s have a simpler code and better-effect code about checking.
Copy the codeThe code is as follows:

<SCRIPT LANGUAGE="JavaScript"> 
<!-- Begin 
function checkAll() { 
for (var j = 1; j <= 9; j++) { 
box = eval("" + j);  
if ( == false)  = true; 
   } 


function uncheckAll() { 
for (var j = 1; j <= 9; j++) { 
box = eval("" + j);  
if ( == true)  = false; 
   } 


function switchAll() { 
for (var j = 1; j <= 9; j++) { 
box = eval("" + j);  
 = !; 
   } 

//  End --> 
</script> 
</head> 
<body> 
<form name=checkboxform> 
<input type=checkbox name=C1>C1<br> 
<input type=checkbox name=C2>C2<br> 
<input type=checkbox name=C3>C3<br> 
<input type=checkbox name=C4>C4<br> 
<input type=checkbox name=C5>C5<br> 
<input type=checkbox name=C6>C6<br> 
<input type=checkbox name=C7>C7<br> 
<input type=checkbox name=C8>C8<br> 
<input type=checkbox name=C9>C9<br> 
<br> 
<input type=button value="Select all" onClick="checkAll()"><br>
<input type=button value="Cancel" onClick="uncheckAll()"><br>
<input type=button value="Anti-select" onClick="switchAll()"><br>
</form>