<!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> |