Js动态生成checkbox(使用Json数据)

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>根据Json动态生成checkbox</title>  
  6.   
  7.     <style type="text/css">  
  8.   
  9.         body {  
  10.             margin: 0 auto;  
  11.         }  
  12.   
  13.         #show {  
  14.             margin: 10px auto;  
  15.             width: 450px;  
  16.             font-size: 14px;  
  17.         }  
  18.   
  19.         .content {  
  20.             float: left;  
  21.             width: 450px;  
  22.             margin: 10px 0px 20px 10px;  
  23.         }  
  24.   
  25.         .bigfont {  
  26.             font-size: 14px;  
  27.         }  
  28.   
  29.         .la {  
  30.             float: left;  
  31.             width: 50px;  
  32.             height: 20px;  
  33.             margin: 5px 0;  
  34.             line-height: 20px;  
  35.         }  
  36.   
  37.         .la input {  
  38.             float: left;  
  39.         }  
  40.   
  41.         .la span {  
  42.             float: left;  
  43.             line-height: 20px;  
  44.         }  
  45.   
  46.     </style>  
  47.   
  48.   
  49.     <script type="text/javascript" src="../../libs/jquery/jquery-1.11.2.min.js"></script>  
  50.   
  51.     <script type="text/javascript">  
  52.   
  53.         window.onload = function () {  
  54.             var years = ["2013", "2014"];  
  55.             var months = {  
  56.                 "2013": [12, 11, 10, 9, 8, 7],  
  57.                 "2014": [6, 5, 4, 3, 2, 1]  
  58.             };  
  59.             var yearMonth = [years, months];  
  60.             genCheck(yearMonth);  
  61.         }  
  62.   
  63.   
  64.         function genCheck(yearMonth) {  
  65.             var content = "content";  
  66.             var checkText = "checkbox";  
  67.             var link = "link";  
  68.             var size;  
  69.   
  70.             $("#show").html("");  
  71.   
  72.             var years = yearMonth[0];  
  73.             size = years.length;  
  74.             for (var i = 0; i < years.length; i++) {  
  75.                 genShowContent("show", checkText + i, i, years[i] + "年", content + i);  
  76.             }  
  77.             var monthObj = yearMonth[1];  
  78.   
  79.             for (var i = 0; i < years.length; i++) {  
  80.                 var array = monthObj["" + years[i] + ""];  
  81.                 for (var j = 0; j < array.length; j++) {  
  82.                     genCheckBox(content + i, link + i, array[j], array[j] + "月", i, false);  
  83.                 }  
  84.   
  85.                 var flag = isAllCheck(link + i);  
  86.                 var box = document.getElementById(checkText + i);  
  87.                 if (flag) {  
  88.                     box.checked = true;  
  89.                 } else {  
  90.                     box.checked = false;  
  91.                 }  
  92.                 $("input[name=" + link + i + "]").each(function () {  
  93.                     $(this).unbind();  
  94.                     $(this).change(function () {  
  95.                         var flag = isAllCheck($(this).attr("name"));  
  96.                         var box = document.getElementById(checkText + $(this).attr("parentIndex"));  
  97.                         if (flag) {  
  98.                             box.checked = true;  
  99.                         } else {  
  100.                             box.checked = false;  
  101.                         }  
  102.                     });  
  103.                 });  
  104.             }  
  105.             for (var i = 0; i < size; i++) {  
  106.                 $("#" + checkText + i).unbind();  
  107.                 $("#" + checkText + i).change(function () {  
  108.                     var temp = link + $(this).attr("index");  
  109.                     var p = document.getElementById(checkText + $(this).attr("index"));  
  110.                     var box = document.getElementsByName(temp);  
  111.                     for (var j = 0; j < box.length; j++) {  
  112.                         if (p.checked) {  
  113.                             box[j].checked = true;  
  114.                         } else {  
  115.                             box[j].checked = false;  
  116.                         }  
  117.                     }  
  118.                 });  
  119.             }  
  120.         }  
  121.   
  122.   
  123.         function genCheckBox(id, name, value, showText, parentIndex, isCheck) {  
  124.             if (!isCheck) {  
  125.                 var checkbox = "<div class='la'><input type='checkbox' parentIndex=" + parentIndex + " name=".concat(name).concat(" value=").concat(value).concat(" alt=").concat(showText).concat(" /><span>").concat(showText).concat("</span></div>");  
  126.                 $("#" + id).append(checkbox);  
  127.             } else {  
  128.                 var checkbox = "<div class='la'><input type='checkbox' parentIndex=" + parentIndex + " name=".concat(name).concat(" checked='checked' value=").concat(value).concat(" alt=").concat(showText).concat(" /><span>").concat(showText).concat("</span></div>");  
  129.                 $("#" + id).append(checkbox);  
  130.             }  
  131.         }  
  132.   
  133.   
  134.         function genShowContent(id, checkboxId, index, showText, idName) {  
  135.             var showContent = "<div class='msg'><span class='bigfont'>".concat(showText).concat(": </span><input type='checkbox' index=").concat(index).concat(" id='").concat(checkboxId).concat("'/><span>全选</span><div class='content' id='").concat(idName).concat("' ></div></div>");  
  136.             $("#" + id).append(showContent);  
  137.         }  
  138.   
  139.   
  140.         function isAllCheck(name) {  
  141.             var box = document.getElementsByName(name);  
  142.             for (var j = 0; j < box.length; j++) {  
  143.                 if (!box[j].checked) {  
  144.                     return false;  
  145.                 }  
  146.             }  
  147.             return true;  
  148.         }  
  149.     </script>  
  150. </head>  
  151. <body>  
  152. <div class="show" id="show"></div>  
  153. </body>  
  154. </html>  
  155. 演示地址:http://zhangjikai.com/demo/html/checkbox.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值