SoFunction
Updated on 2025-02-28

js to determine whether checkbox is selected implementation code

Today I will write a small function to determine whether checkbox is selected and the requirement to set checkbox to the selected state after the operation is completed. Then you can refer to the following code

1. You can use the following code to determine whether to select it.

Native js

if(("insDown").checked){

}

jquery implementation code

1, $(checkbox id).prop("checked") returns a boolean value type

2, $(this).is(":checked") returns a boolean value type 2

, set the selected status

Native js

("insDown").checked=true;

jquery implementation code

$("#insDown").prop("checked", true);

$("#cb1").prop("checked","checked");
$("#cb1").prop("checked",true);

Complete code

//Nothing is selected
<input type="checkbox" name="checkbox1" checked> 
<input type="checkbox" name="checkbox1"> 
<input type="checkbox" name="checkbox1" checked> 
<input type="checkbox" name="checkbox1"> 
<input type="checkbox" name="checkbox1"> 
<script language=javascript> 
function check(obj) 
{ 

for(i=0;i<(obj).length;i++) 
{ 
if(!(obj)[i].checked)alert("Third"+(i+1)+"No choice") 
} 
} 
</script> 
<input type=button onclick="check('checkbox1')" value="Detection"> 
//No one is selected<input type="checkbox" name="test"> 
<input type="checkbox" name="test"> 
<input type="checkbox" name="test"> 
<input type="checkbox" name="test"> 
<input type="checkbox" name="test"> 
<Script Language="JavaScript"> 
function check(obj){ 
for(i=0;i<(obj).length;i++){ 
if((obj)(i).checked){ 
return; 
} 
} 
('No choice!  '); 
} 
</Script> 
<input type=button onclick="check('test');" value="Detection"> 

//Nothing is selected<input type="checkbox" name="checkbox1" checked> 
<input type="checkbox" name="checkbox1"> 
<input type="checkbox" name="checkbox1" checked> 
<input type="checkbox" name="checkbox1"> 
<input type="checkbox" name="checkbox1"> 
<script language=javascript> 
function check(obj) 
{ 

for(i=0;i<(obj).length;i++) 
{ 
if(!(obj)[i].checked)alert("Third"+(i+1)+"No choice") 
} 
} 
</script> 
<input type=button onclick="check('checkbox1')" value="Detection"> 


//No one is selected

<input type="checkbox" name="test"> 
<input type="checkbox" name="test"> 
<input type="checkbox" name="test"> 
<input type="checkbox" name="test"> 
<input type="checkbox" name="test"> 
<Script Language="JavaScript"> 
function check(obj){ 
for(i=0;i<(obj).length;i++){ 
if((obj)(i).checked){ 
return; 
} 
} 
('No choice!  '); 
} 
</Script> 
<input type=button onclick="check('test');" value="Detection">

This article is over, please check out my previous posts for more.