> with a value of 1. And you have five of these..
> a_1 = 0
> a_2 = 1
> a_3 = 0
> a_4 = 0
> a_5 = 1
> how would you keep track of the totals?
> Initially you could just do a sum of the values but what if
> the user changed an option? it wouldnt subtract the 1?
> Any ideas would be appreciated.
You can assign an event handler to any form field, so that when an event
occurs a _javascript_ function is called. For example, if you had an array of
checkboxes, you could loop over them to see which ones were checked. You
could do this whenever any of them were checked or unchecked.
<script>
function calcTotal() {
var total = 0;
for (var i = 0; i < document.forms[0].items.length; i++) {
if (document.forms[0].items[i].checked) {
total += document.forms[0].items[i].value;
}
}
document.forms[0].total.value = total;
}
</script>
<form ...>
<input type="checkbox" name="items" value="1" 1<br>
<input type="checkbox" name="items" value="4" 4<br>
<input type="text" name="total">
</form>
Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
phone: 202-797-5496
fax: 202-797-5444
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]