<!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=gb2312" /> <title>无标题文档</title> <script type="text/javascript"> function getSum() { var items = document.getElementsByName("item"); var sum = 0; for(var x = 0; x < items.length; x++) { if(items[x].checked) { sum += parseInt(items[x].value); } } var str = sum + "元"; document.getElementById("sumid").innerHTML = str.fontcolor('red'); } function checkAll(node) { var items = document.getElementsByName("item"); for(var x = 0; x < items.length; x++) { items[x].checked = node.checked; } } </script> </head> <body> <div>商品列表</div> <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()" /><span id="sumid"></span> </body> </html>
运行结果: