PDA

View Full Version : How do I calculate values of check boxes.


malhyp
12-27-2005, 03:17 AM
Hey there, can anyone suggest how I would calculate the values of 6 check boxes I have on a page.

Eg: There are 6 items c customer can purchase on one page. Each item has a check box so that the client can click on each one. If the client clicks on 3 check boxes, I would like the total to come up straight away on the same page. Is this possible.

I am using DWMX04 & ASP.

Thanks

davidj
12-29-2005, 09:31 AM
<script LANGUAGE="JavaScript">


function CheckChoice(whichbox)
{
with (whichbox.form)
{
//Handle differently, depending on type of input box.

if (whichbox.checked == false)
{ hiddentotal.value = eval(hiddentotal.value) - eval(whichbox.value); }
else { hiddentotal.value = eval(hiddentotal.value) + eval(whichbox.value); }


//Ensure the total never goes negative
if (hiddentotal.value < 0)
{
InitForm();
}

//Now, return with formatted total:
return(formatCurrency(hiddentotal.value));
}
}

//Define function to format a value as currency:
function formatCurrency(num)
{
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
pence = num%100;
num = Math.floor(num/100).toString();
if(pence<10)
pence = "0" + pence;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '' + num + '.' + pence);
}

//Define function to init the form on reload:
function InitForm()
{
//Reset the displayed total on form:
document.myform.total.value='0';
document.myform.hiddentotal.value=0;

//Set all checkboxes to unchecked:
for (xx=0; xx < document.myform.elements.length; xx++)
{
document.myform.elements[xx].checked = false;
}



}

// End -->
</script>


<BODY onLoad="InitForm();" onreset="InitForm();">

<form method="POST" name="myform">
<font face=Arial size=2>
<input name="no1" type="checkbox" id="no1" onclick="this.form.total.value=CheckChoice(this);" value=15.25>
<input name="no2" type="checkbox" id="no2" onClick="this.form.total.value=CheckChoice(this);" value=12.39>
<input name="no3" type="checkbox" id="no3" onclick="this.form.total.value=CheckChoice(this);" value=18.75>
<br><br>
<br>
<br>
<input type="hidden" name="hiddentotal" value=0>
<font size=+1>
Your total is: <input type="text" name="total" readonly onFocus="this.blur();">
</font>
<br>
</font>
</form>

malhyp
12-29-2005, 10:34 PM
David, it worked perfectly.

Thanks again.

Heppy new year.