• 全选、反选


    项目中经常会用到全选和反选功能,这里记录一个示例
    <!
    DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery.min.js"></script> </head> <body> <input type="checkbox" id="all"> <input type="button" value="全选" class="btn" id="selectAll"> <input type="button" value="全不选" class="btn" id="unSelect"> <input type="button" value="反选" class="btn" id="reverse"> <input type="button" value="获得选中的所有值" class="btn" id="getValue"> <ul id="list"> <li><input type="checkbox" value="1"> 1.时间都去哪儿了</li> <li><input type="checkbox" value="2"> 2.海阔天空</li> <li><input type="checkbox" value="3"> 3.真的爱你</li> <li><input type="checkbox" value="4"> 4.不再犹豫</li> <li><input type="checkbox" value="5"> 5.光辉岁月</li> <li><input type="checkbox" value="6"> 6.喜欢妳</li> </ul> <script type="text/javascript"> $(document).ready(function() { //全选或全不选方式一 /* $("#all").click(function(){ //alert(this.checked); if(this.checked){ $("#list :checkbox").prop("checked",true); }else{ $("#list :checkbox").prop("checked",false); } }); */ //全选或全不选方式二 $("#all").click(function(){ var boxes = $("#list :checkbox"); alert(boxes.length) if(this.checked){ for(var i=0;i<boxes.length;i++){ boxes[i].checked=true; } }else{ for(var i=0;i<boxes.length;i++){ boxes[i].checked=false; } } }); //全选 $("#selectAll").click(function () { $("#list :checkbox,#all").prop("checked", true); }); //全不选 $("#unSelect").click(function(){ $("#list :checked,#all").prop("checked",false); }); //反选 $("#reverse").click(function () { $("#list :checkbox").each(function () { $(this).prop("checked", !$(this).prop("checked")); }); allchk(); }); //设置全选复选框 $("#list :checkbox").click(function(){ allchk(); }); }); function allchk(){ var chknum = $("#list :checkbox").size();//选项总个数 var chk = 0; $("#list :checkbox").each(function () { if($(this).prop("checked")==true){ chk++; } }); if(chknum==chk){//全选 $("#all").prop("checked",true); }else{//不全选 $("#all").prop("checked",false); } } </script> </body> </html>
  • 相关阅读:
    点双连通分量
    CF 839 E-最大团
    线性基
    CF 859 E
    【bzoj1303】[CQOI2009]中位数图
    【bzoj2282】[Sdoi2011]消防
    【bzoj2654]】tree
    【bzoj2600】 [Ioi2011]ricehub
    【bzoj4027】 [HEOI2015]兔子与樱花
    【bzoj3670】 [Noi2014]动物园
  • 原文地址:https://www.cnblogs.com/guzhou-ing/p/7204063.html
Copyright © 2020-2023  润新知