Introduction to the use of 'all-check' in jQuery

  • 2020-03-29 23:41:16
  • OfStack

Page code
 
<body> 
<div> 
<input id="checkall" type="checkbox" value=" day " /> all <br/> 
<div id="con"> 
<input type="checkbox" name="checkbox" value=" day " /> day  
<input type="checkbox" name="checkbox" value=" empty " /> empty  
<input type="checkbox" name="checkbox" value=" fly " /> fly  
<input type="checkbox" name="checkbox" value=" to " /> to  
<input type="checkbox" name="checkbox" value=" five " /> five  
<input type="checkbox" name="checkbox" value=" a " /> a  
<input type="checkbox" name="checkbox" value=" word " /> word  
<input type="checkbox" name="checkbox" value=" this " /> this  
<input type="checkbox" name="checkbox" value=" all " /> all  
<input type="checkbox" name="checkbox" value=" Don't " /> Don't  
<input type="checkbox" name="checkbox" value=" calculate " /> calculate  
<input type="checkbox" name="checkbox" value=" things " /> things </div> 
</div> 
<script src="Scripts/jquery-1.4.1.js" language="javascript"></script> 
<script src="JqueryAll.js" language="javascript"></script> 
</body> 

Code in js
 
/*js Part of the  
*  Binding batch selection / Not to choose  
*  example  
*/ 

$(function () { 
$("#checkall").click(function () { 
var stu = $(this).attr("checked"); 
checkAll(stu); 
}) 
$("input[name='checkbox']").click(function () { 
uncheckAll(); 
}) 
}) 
function checkAll(status) { 
$("input[name='checkbox']").each(function () { 
$(this).attr("checked"); 
$(this).attr("checked", status); 
}) 
} 

function uncheckAll() { 
alert($("input[name='checkbox']:checked").length); 
if ($("input[name='checkbox']:checked").length == $("input[name='checkbox']").length) { 
$("#checkall").attr("checked",true); 
} 
else { 
$("#checkall").attr("checked", false); 
} 
} 

Related articles: