I have the following code generated from a database, I want to have javascript calculate the
costs of the selected items using radio buttons, subtotal the costs and grandtotal the costs
ready for the selected items to be inserted back to the database. I did something like this
before with Checkboxes, but Radio button have to be named the same to maintain their
groupings.
Radio Button index is generated from the recordset using asp
Expand|Select|Wrap|Line Numbers
- <td>
- <%=(EquiptRS.Fields.Item("EquiptDescr").Value)%>
- <input name="descr1" type="hidden" id="descr1"
- value="<%=(EquiptRS.Fields.Item("EquiptDescr").Value)%>" />
- <input name="quantity<%=(EquiptRS.Fields.Item("EquipmentID").Value)-1%>"
- type="hidden" id="quantity<%=(EquiptRS.Fields.Item("EquipmentID").Value)-1%>" value="1.00"
- /></td>
- <td><input name="price<%=(EquiptRS.Fields.Item("EquipmentID").Value)-1%>" type="radio"
- onClick="total(this.form,<%=(EquiptRS_total)%>)" value="<%=
- FormatNumber((EquiptRS.Fields.Item("day1").Value), 2, -2, -2, -2) %>" />
- <%= FormatNumber((EquiptRS.Fields.Item("day1").Value), 2, -2, -2, -2) %>
- </td>
- This creates an array from the EquipmentID and RS RecordCount from the database
- Can I use id="DifferentName" to name="price[andgeneratedname]"? do i use GetElementByID to
- check if the element is checked or not?
- If I name all "name" elements the same I get NaN in the subtotals;
- Here is my code:
- <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
- //-->
- function roundoff(amount) {
- return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 ==
- Math.floor(amount*10)) ? amount + '0' : amount);
- }
- function total(what,number) {
- var grandTotal = 0;
- for (var i=0;i<number;i++) {
- if (what.elements['price' + i].checked)
- what.elements['price' + i].checked;
- what.elements['subtotal' + i].value=roundoff((what.elements['quantity' + i].value - 0) *
- (what.elements['price' + i].value - 0));
- if (what.elements['quantity' + i].value == "0")
- what.elements['subtotal' + i].value = "0.00";
- subtotal=roundoff(what.elements['subtotal' + i].value)
- grandTotal += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value
- - 0);
- }
- //write the totals into the what.elements
- what.subtotal = roundoff(Math.round(subtotal*Math.pow(10,2))/Math.pow(10,2));
- what.grandTotal.value = roundoff(Math.round(grandTotal*Math.pow(10,2))/Math.pow(10,2));
- }
- </SCRIPT>
- Here is the HTML generated from the database:
- <form id="addrange" name="addrange">
- <table width="100%" border="0" cellpadding="3">
- <tr>
- <td width="19%"> </td>
- <td width="4%">Qty</td>
- <td width="9%">1 Day </td>
- <td width="9%">2 Days </td>
- <td width="9%">3 Days</td>
- <td width="9%">4 Days </td>
- <td width="10%">5 Days </td>
- <td width="10%">6 Days</td>
- <td width="9%">6+Days </td>
- <td width="12%">subtotal</td>
- </tr>
- <tr>
- <td>Standard carving ski </td>
- <td><input name="quantity0" type="text" id="quantity0" value="1.00" size="2" /></td>
- <td><input name="price0" id="pr1" type="radio" onclick="total(this.form,10)"
- value="8.50" />
- 8.50</td>
- <td><input name="price0" id="pr2" type="radio" onclick="total(this.form,10)"
- value="16.00" />
- 16.00</td>
- <td><input name="price0" id="pr3" type="radio" value="23.00"
- onclick="total(this.form,10)" />
- 23.00</td>
- <td><input name="price0" id="pr4" type="radio" value="30.00"
- onclick="total(this.form,10)" />
- 30.00</td>
- <td><input name="price0" id="pr5" type="radio" value="37.00"
- onclick="total(this.form,10)" />
- 37.00</td>
- <td><input name="price0" id="pr6" type="radio" value="42.00"
- onclick="total(this.form,10)" />
- 42.00</td>
- <td>7.00</td>
- <td><input name="subtotal0" type="text" id="subtotal0" value="0.00" size="5" /></td>
- </tr>
- <tr>
- <td>Snow blade </td>
- <td><input name="quantity1" type="text" id="quantity1" value="1.00" size="2" /></td>
- <td><input name="price1" id="pr1" type="radio" onclick="total(this.form,10)"
- value="8.50" />
- 8.50</td>
- <td><input name="price1" id="pr2" type="radio" onclick="total(this.form,10)"
- value="16.00" />
- 16.00</td>
- <td><input name="price1" id="pr3" type="radio" value="23.00"
- onclick="total(this.form,10)" />
- 23.00</td>
- <td><input name="price1" id="pr4" type="radio" value="30.00"
- onclick="total(this.form,10)" />
- 30.00</td>
- <td><input name="price1" id="pr5" type="radio" value="37.00"
- onclick="total(this.form,10)" />
- 37.00</td>
- <td><input name="price1" id="pr6" type="radio" value="42.00"
- onclick="total(this.form,10)" />
- 42.00</td>
- <td>7.00</td>
- <td><input name="subtotal1" type="text" id="subtotal1" value="0.00" size="5" /></td>
- </tr>
- <tr>
- <td>Superior carving ski </td>
- <td><input name="quantity2" type="text" id="quantity2" value="1.00" size="2" /></td>
- <td><input name="price2" id="pr1" type="radio" onclick="total(this.form,10)"
- value="14.00" />
- 14.00</td>
- <td><input name="price2" id="pr2" type="radio" onclick="total(this.form,10)"
- value="27.00" />
- 27.00</td>
- <td><input name="price2" id="pr3" type="radio" value="36.50"
- onclick="total(this.form,10)" />
- 36.50</td>
- <td><input name="price2" id="pr4" type="radio" value="46.50"
- onclick="total(this.form,10)" />
- 46.50</td>
- <td><input name="price2" id="pr5" type="radio" value="55.00"
- onclick="total(this.form,10)" />
- 55.00</td>
- <td><input name="price2" id="pr6" type="radio" value="63.00"
- onclick="total(this.form,10)" />
- 63.00</td>
- <td>10.50</td>
- <td><input name="subtotal2" type="text" id="subtotal2" value="0.00" size="5" /></td>
- </tr></table></form>
same, but then all subtotals show NAN.
If I uniquely name the radio buttons (but interestingly leave the last one as is) the result
in that rows subtotal box returns the last ones value correctly,but then the radio buttons
dont work correctly.
Hope you can help.