我有兩個或多個表單。並且每個表單都包含一些複選框以及文本框中顯示的檢查值。我想,當我che一些複選框的總價值將顯示在一個文本框中的形式。 我希望當我檢查一個窗體的複選框時,其他窗體的複選框將不會被檢查並返回警報。 這裏是我的代碼:比較兩個表單的複選框是否被選中
<head>
<script>function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=0; i<5; i++) {
gn = 'budget'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById('totalcost').value = sum.toFixed(2);
}
</script>
</head>
<body>
// 1st form
<form name= "form1">
<input type="checkbox" id='budget0' value="9.99" onclick="UpdateCost()">Game 1 (9.99)<br>
<input type="checkbox" id='budget1' value="19.99" onclick="UpdateCost()">Game 2 (19.99)<br>
<input type="checkbox" id='budget2' value="27.50" onclick="UpdateCost()">Game 3 (27.50)<br>
<input type="checkbox" id='budget3' value="45.65" onclick="UpdateCost()">Game 4 (45.65)<br>
<input type="checkbox" id='budget4' value="87.20" onclick="UpdateCost()">Game 5 (87.20)<br>
<input type="text" id="totalcost" value="">// total budget
</form>
// 2nd form
<form name= "form2">
<input type="checkbox" id='budget0' value="9.99" onclick="UpdateCost()">Game 1 (9.99)<br>
<input type="checkbox" id='budget1' value="19.99" onclick="UpdateCost()">Game 2 (19.99)<br>
<input type="checkbox" id='budget2' value="27.50" onclick="UpdateCost()">Game 3 (27.50)<br>
<input type="checkbox" id='budget3' value="45.65" onclick="UpdateCost()">Game 4 (45.65)<br>
<input type="checkbox" id='budget4' value="87.20" onclick="UpdateCost()">Game 5 (87.20)<br>
<input type="text" id="totalcost" value="">// total budget
</form>
</body>
我需要澄清什麼,或者我的新代碼是否滿足這個問題? – Samuel 2011-05-05 16:40:10