i’m getting nuts with this, i got a lame function to work but is not compatible with all browsers:
javascript code:
function KeepCount() {
var NewCount = 0;
if (document.FormName.iphone3g1.checked)
{NewCount = NewCount + 1;}
if (document.FormName.iphone3g2.checked)
{NewCount = NewCount + 1;}
if (document.FormName.iphone3g3.checked)
{NewCount = NewCount + 1;}
if (document.FormName.iphone3g4.checked)
{NewCount = NewCount + 1;}
if (NewCount == 2){
valor.value = total.value - (total.value*20/100);}
if (NewCount <= 1){
valor.value = 0;
}
}
html part:
<input onclick="clickCh(this);return KeepCount();" type="checkbox" name="iphone3g1" value="50.00"> Vidro Partido<br />
<input onclick="clickCh(this);return KeepCount();" type="checkbox" name="iphone3g2" value="59.00"> LCD Danificado<br />
<input onclick="clickCh(this);return KeepCount();" type="checkbox" name="iphone3g3" value="80.00"> Substituir capa traseira<br />
<input onclick="clickCh(this);return KeepCount();" type="checkbox" name="iphone3g4" value="38.00"> Botão Volume
Well, this applies a disccount using chrome and firefox, but i don’t really like the code anyway.
I know there are a lot of experts here, can some of you help me, please? I would be very thankful
If you use jquery, you could do something like
That way you don’t have manually code each checkbox. Instead wrap them all in some div, or give them some unique css class, in which case you’d select with
$('input.myProduct:checked'), and its easy to get the count.(BTW, you probably wantNewCount >= 2, not==)As @John Weldon said though, it’s fine to calculate this client side if you want to display it in the browser, but you MUST verify that all the prices are proper on the server side once you receive the order, otherwise I could give myself a 100% discount, etc…