js to determine whether the checkbox is selected

Core tip: In many occasions, you may encounter judging whether there is an element selected on the page. The following describes the method of using js to judge whether the CheckBox is selected.

//The first few are not 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<document.all(obj).length;i++)       
{       
if(!document.all(obj)[i].checked)alert("第"+(i+1)+"个没有选择")       
}       
}       
</script>       
<input type=button onclick="check('checkbox1')" value="test">       <input type="checkbox" name="test">       
      
      
//None is selected       
      
      

<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<document.all(obj).length;i++){       
if(document.all(obj)(i).checked){       
return;       
}       
}       
window.alert('一个也没有选!');       
}       
</Script>       
<input type=button onclick="check('test');" value="检测">       
   
//第几个没有选    
<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<document.all(obj).length;i++)    
{    
if(!document.all(obj)[i].checked)alert("第"+(i+1)+"个没有选择")    
}    
}    
</script>    
<input type=button onclick="check('checkbox1')" value="检测">    
   
   
//一个也没有选    
   
   
<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<document.all(obj).length;i++){    
if(document.all(obj)(i).checked){    
return;    
}    
}    
window.alert('一个也没有选!');    
}    
</Script>    
<input type=button onclick="check('test');" value="检测">    

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326983044&siteId=291194637