Код ниже добавляет суммы флажков и дает скидку, если отмечены ВСЕ. И у меня это работает отлично, но по какой-то причине, когда я использую кнопку «ПРОВЕРИТЬ ВСЕ», а затем вручную сниму флажок с одного из полей, он устанавливает общее значение -119, а не 476. Я уверен, что я что-то упустил, но я не могу понять это. Любая помощь будет оценена!
Спасибо.
<!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>
<link rel="shortcut icon" type="image/x-icon" href="../favicon.ico" />
<link rel="icon" type="image/x-con" href="../favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- Begin JavaScript -->
<!--- ADDING THE CHECK BOXES WITH SELECT ALL DISCOUNT --->
<script language="javascript" type="text/javascript">
//Define global form total:
var form_amount=0;
//Define function to check if all boxes are selected:
function allChecked()
{
for (xx=0; xx < document.WisBundle.elements.length; xx++)
{
if (document.WisBundle.elements[xx].type == 'checkbox' && document.WisBundle.elements[xx].checked == false)
return false;
if (checked == false){
form_amount = 0;
for (i = 0; i < document.WisBundle.elements.length; i++)
{
if (document.WisBundle.elements[i].type == 'checkbox') {
form_amount += parseFloat(document.WisBundle.elements[i].value);
}
}
checked = true;
document.WisBundle.amount.value = form_amount;
}
}
return true;
}
//Define function to manipulate the form total per item selected/deselected:
function CheckChoice(whichbox)
{
//If box was checked, accumulate the checkbox value as the form total,
//Otherwise, reduce the form total by the checkbox value:
if (whichbox.checked == false)
{ form_amount -= eval(whichbox.value); }
else { form_amount += eval(whichbox.value); }
//Re-set displayed total on form:
document.WisBundle.amount.value = eval(form_amount);
//If all the boxes are checkec, display the total as 999.00 giving them a discount:
if(allChecked())
document.WisBundle.amount.value = '999.00';
else
//Re-set displayed total on form:
document.WisBundle.amount.value = eval(form_amount);
}
//Define function to init the form on reload:
function InitForm()
{
//Reset the displayed total on form:
document.WisBundle.amount.value='0';
//Set all checkboxes on form to unchecked:
for (xx=0; xx < document.WisBundle.elements.length; xx++)
{
if (document.WisBundle.elements[xx].type == 'checkbox')
{
document.WisBundle.elements[xx].checked = false;
}
}
}
//Check and uncheck all checkboxes:
checked=false;
function checkedAll () {
var aa= document.WisBundle;
if (checked == false)
{
checked = true
document.WisBundle.amount.value = '999.00';
}
else
{
checked = false
document.WisBundle.amount.value = '0';
form_amount = 0;
}
//white checked is true do this
for (var i =0; i < aa.elements.length; i++)
{
aa.elements[i].checked = checked;
}
}
</script>
<title>Add Boxes</title>
</head>
<body>
<form action="BLANKPAGE_TEST_2.cfm" method="post" id="addCommentForm" name="WisBundle" onsubmit="return submit_form()">
<input type="button" name="UnCheckAll" value="Check / Uncheck All" onclick='checkedAll();'>
<!--- <label><input type='checkbox' name='checkall' onclick='checkedAll();'> Select All</label> --->
<hr />
<table id="commentTable">
<tr>
<th><label>Item One</label></th>
<td>
<label><input type="checkbox" name="ItemOne" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /> $119</label></td>
</tr>
<tr>
<th><label>Item Two</label></th>
<td>
<label><input type="checkbox" name="ItemTwo" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /> $119</label></td>
</tr>
<tr>
<th><label>Item Three</label></th>
<td>
<label><input type="checkbox" name="ItemThree" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /> $119</label></td>
</tr>
<tr>
<th><label>Item Four</label></th>
<td>
<label><input type="checkbox" name="ItemFour" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /> $119</label></td>
</tr>
<tr>
<th><label>Item Five</label></th>
<td>
<label><input type="checkbox" name="ItemFive" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /> $119</label></td>
</tr>
<tr>
<th class="altTH"><label>Total $:</label></th>
<td class="altTD"><input type="text" name="amount" readonly="readonly" class="inputSmall" /></td>
</tr>
</table>
</form>
</body>
</html>