日期:2014-05-16 浏览次数:20384 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript"> function getSum(){ var items = byName("item"); var sum=0; for(var x=0;x<items.length;x++) { if(items[x].checked) { sum+=parseInt(items[x].value); } } var spanNode = getElementById("sum"); spanNode.innerHTML = sum+"元"; } function checkAll(allNode) { var allNode = getElementsByName("all")[0]; var items = getElementsByName("item"); var sum =0; for(var x=0; x<items.length; x++) { if(allNode.checked) { items[x].checked = allNode.checked; } } } </script> </head> <body> <input type="checkbox" name="all" onclick="checkAll(this)" />全选<br/> <input type="checkbox" name="item" value="3000" />笔记本电脑:3000<br/> <input type="checkbox" name="item" value="3000" />笔记本电脑:3000<br/> <input type="checkbox" name="item" value="3000" />笔记本电脑:3000<br/> <input type="checkbox" name="item" value="3000" />笔记本电脑:3000<br/> <input type="checkbox" name="item" value="3000" />笔记本电脑:3000<br/> <input type="checkbox" name="item" value="3000" />笔记本电脑:3000<br/> <input type="checkbox" name="all" onclick="checkAll(this)" />全选<br/> <input type="button" value="获取总金额" onclick="getSum()" /><br/><span id="sum"></span> </body> </html>
function checkAll(allNode) { var allNode = getElementsByName("all")[0];//这里你一直都只取第一个全选来判断 第二个肯定没问题的了 var items = getElementsByName("item"); var sum =0; for(var x=0; x<items.length; x++) { if(allNode.checked) { items[x].checked = allNode.checked; } } }