Checkbox with Parsed Value amount
Posted: Thu Oct 01, 2009 10:33 am
I need help with some code. I have a checkbox form with Quantity and I need it to total at the bottom. I can get the code to do just a checkbox with a set value but since the value has more data than just a total I need it parsed. Also, I cant get the total to work with both Qty(textbox) and checkbox changing.
The value of the checkbox will follow this format: "acct^partn^desc^price^qty^na^na^na^ship weight"
the sections between qty and ship weight, labeled "na" will 99.99% of the time not be used but still has to be sent as an empty value -
example: "acc1t^abc123^blahblah^10.15^1^^^^5"
<---- Me
here is what I have:
The value of the checkbox will follow this format: "acct^partn^desc^price^qty^na^na^na^ship weight"
the sections between qty and ship weight, labeled "na" will 99.99% of the time not be used but still has to be sent as an empty value -
example: "acc1t^abc123^blahblah^10.15^1^^^^5"
here is what I have:
Code: Select all
<script type="text/javascript">
function updatetotal() {
document.main.total.value = '';
var sum = 0;
for (i=0;i<document.main.choice.length;i++) {
if (document.main.choice[i].checked) {
// value is acct, partn, desc, price, qty, na, na, na, ship weight
var ar = new String(document.main.choice[i].value);
var ara = Array(ar.split('^'));
var val = ara(3);
var qty = ara(4);
sum = sum + parseInt(val * qty);
ar = document.main.choice[i].value;
}
}
document.main.total.value = sum;
document.main.tv.value = val;
document.main.tq.value = qty;
}
</script>
<form name="main">
<p>test a: Qty: <input type='TEXT' name='op1' value='1' size='2' onchange="updatetotal()" />
Select:
<input type="checkbox" name="choice1" value="acc1t^abc123^desc blah1^20.10^op1^^^^14" onchange="updatetotal()"/>$20.10<br/>
test b: Qty: <input type='TEXT' name='op2' value='1' size='2' onchange="updatetotal()" />
Select:
<input type="checkbox" name="choice2" value="acct1^abc124^desc blah2^30.20^op2^^^^24" onchange="updatetotal()"/>$30.20<br/>
test c: Qty: <input type='TEXT' name='op3' value='1' size='2' onchange="updatetotal()" />
Select:
<input type="checkbox" name="choice3" value="acct1^abc125^desc blah3^40.30^op3^^^^34" onchange="updatetotal()"/>$40.30<br/>
test d: Qty: <input type='TEXT' name='op4' value='1' size='2' onchange="updatetotal()" />
Select:
<input type="checkbox" name="choice4" value="acct1^abc126^desc blah4^50.40^op4^^^^44" onchange="updatetotal()"/>$50.40<br/>
</p>
<p>Total: <input type="text" size="5" name="total" value="0"/> /
test a: <input type="text" size="5" name="tv" value="0"/>
test b: <input type="text" size="5" name="tq" value="0"/>
</p>
</form>