<html> <head> <title> </title> <script> function static_num() { document.getElementById("btnOperate").onclick = function () { var arr = new Array(); var items = document.getElementsByName("category"); for (i = 0; i < items.length; i++) { if (items[i].checked) { arr.push(items[i].value); } } alert("选择的个数为:" + arr.length); }; }; </script> </head> <body> <p><input type="checkbox" name="category" value="今日话题" />今日话题 </p> <p><input type="checkbox" name="category" value="视觉焦点" />视觉焦点</p> <p><input type="checkbox" name="category" value="财经" />财经</p> <p><input type="checkbox" name="category" value="汽车" />汽车</p> <p><input type="checkbox" name="category" value="科技" />科技</p> <p><input type="checkbox" name="category" value="房产" />房产</p> <p><input type="checkbox" name="category" value="旅游" />旅游</p> <p><input id="btnOperate" type="button" value="选择" onclick="static_num()" /></p> </body> </html>
效果
<html> <head> <title> </title> <script> function mini(txt) { document.getElementById(‘show_text‘).innerHTML = txt; } </script> </head> <body> <input type="checkbox" name="category" value="今日话题" onclick="mini(‘你好‘)"/>今日话题 <div id="show_text"> </div> </body> </html>
效果
<html> <head> <title> </title> <script> function my_func() { var items = document.getElementsByName("category"); document.getElementById("node" + String(1)).innerHTML = "node1" + ":" + items[0].checked; document.getElementById("node2" ).innerHTML = "node2" + ":" + items[1].checked; } </script> <div id="node1">node1</div> <div id="node2">node2</div> </head> <body> <p><input type="checkbox" checked name="category" value="今日话题" onclick="my_func()"/>今日话题 </p> <p><input type="checkbox" checked name="category" value="视觉焦点" onclick="my_func()"/>视觉焦点</p> </body> </html>
效果
执行前
执行后
原文:http://www.cnblogs.com/kaituorensheng/p/4529113.html