<!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>网页特效 计算选中的多选框checkbox值的总和</title> | |
<!--把下面代码加到<head>与</head>之间--> | |
<script type="text/javascript"> | |
var sum=0; | |
function shops(o){ | |
sum+=parseInt(o.checked?o.value:-o.value);//如果选中就加选中的那个复选框的值,否则就减去 | |
document.getElementById('gong').innerHTML='共'+sum+'元'; | |
} | |
</script> | |
</head> | |
<body> | |
<form name="form1" action="" target="_blank"> | |
<input name="shop" id="shop_1" type="checkbox" value="10" onclick="shops(this)"><label for="shop_1">10元</label> | |
<input name="shop" id="shop_2" type="checkbox" value="20" onclick="shops(this)"><label for="shop_2">20元</label> | |
<input name="shop" id="shop_3" type="checkbox" value="30" onclick="shops(this)"><label for="shop_3">30元</label> | |
<input name="shop" id="shop_4" type="checkbox" value="40" onclick="shops(this)"><label for="shop_4">40元</label> | |
<input name="shop" id="shop_5" type="checkbox" value="50" onclick="shops(this)"><label for="shop_5">50元</label> | |
<input type="submit" name="submit1" value="提交"> | |
</form> | |
<div id="gong">共0元</div> | |
</body> | |
</html> |