469,649 Members | 1,187 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,649 developers. It's quick & easy.

Javascript and asp radio button array

Hi, can anyone help here?
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
  1. <td>
  2. <%=(EquiptRS.Fields.Item("EquiptDescr").Value)%>
  3.         <input name="descr1" type="hidden" id="descr1" 
  4.  
  5. value="<%=(EquiptRS.Fields.Item("EquiptDescr").Value)%>" />
  6.         <input name="quantity<%=(EquiptRS.Fields.Item("EquipmentID").Value)-1%>" 
  7.  
  8. type="hidden" id="quantity<%=(EquiptRS.Fields.Item("EquipmentID").Value)-1%>" value="1.00" 
  9.  
  10. /></td>
  11.     <td><input name="price<%=(EquiptRS.Fields.Item("EquipmentID").Value)-1%>" type="radio" 
  12.  
  13. onClick="total(this.form,<%=(EquiptRS_total)%>)" value="<%= 
  14.  
  15. FormatNumber((EquiptRS.Fields.Item("day1").Value), 2, -2, -2, -2) %>" />
  16.         <%= FormatNumber((EquiptRS.Fields.Item("day1").Value), 2, -2, -2, -2) %>
  17. </td>
  18.  
  19. This creates an array from the EquipmentID and RS RecordCount from the database
  20.  
  21. Can I use id="DifferentName" to name="price[andgeneratedname]"? do i use GetElementByID to 
  22.  
  23. check if the element is checked or not?
  24. If I name all "name" elements the same I get NaN in the subtotals;
  25. Here is my code:
  26.  
  27. <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
  28. //-->
  29. function roundoff(amount) {
  30. return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == 
  31.  
  32. Math.floor(amount*10)) ? amount + '0' : amount);
  33. }
  34.  
  35. function total(what,number) {
  36. var grandTotal = 0;
  37.  
  38. for (var i=0;i<number;i++) {
  39. if (what.elements['price' + i].checked)
  40. what.elements['price' + i].checked;
  41.  
  42.  
  43. what.elements['subtotal' + i].value=roundoff((what.elements['quantity' + i].value - 0) * 
  44.  
  45. (what.elements['price' + i].value - 0));
  46. if (what.elements['quantity' + i].value == "0")
  47. what.elements['subtotal' + i].value = "0.00";
  48.  
  49. subtotal=roundoff(what.elements['subtotal' + i].value)
  50. grandTotal += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value 
  51.  
  52. - 0);
  53.  
  54. }
  55. //write the totals into the what.elements
  56. what.subtotal = roundoff(Math.round(subtotal*Math.pow(10,2))/Math.pow(10,2));
  57. what.grandTotal.value = roundoff(Math.round(grandTotal*Math.pow(10,2))/Math.pow(10,2));
  58.  
  59. }
  60. </SCRIPT>
  61.  
  62. Here is the HTML generated from the database:
  63.  
  64. <form id="addrange" name="addrange">
  65. <table width="100%" border="0" cellpadding="3">
  66.   <tr>
  67.  
  68.     <td width="19%">&nbsp;</td>
  69.     <td width="4%">Qty</td>
  70.     <td width="9%">1 Day    </td>
  71.     <td width="9%">2 Days    </td>
  72.     <td width="9%">3 Days</td>
  73.     <td width="9%">4 Days </td>
  74.     <td width="10%">5 Days </td>
  75.  
  76.     <td width="10%">6 Days</td>
  77.     <td width="9%">6+Days </td>
  78.     <td width="12%">subtotal</td>
  79.   </tr>
  80.  
  81.   <tr>
  82.     <td>Standard carving ski        </td>
  83.     <td><input name="quantity0" type="text" id="quantity0" value="1.00" size="2" /></td>
  84.     <td><input name="price0" id="pr1" type="radio" onclick="total(this.form,10)" 
  85.  
  86. value="8.50" />
  87.  
  88.         8.50</td>
  89.     <td><input name="price0" id="pr2" type="radio" onclick="total(this.form,10)" 
  90.  
  91. value="16.00"  />
  92.         16.00</td>
  93.     <td><input name="price0" id="pr3" type="radio" value="23.00" 
  94.  
  95. onclick="total(this.form,10)" />
  96.         23.00</td>
  97.     <td><input name="price0" id="pr4" type="radio" value="30.00" 
  98.  
  99. onclick="total(this.form,10)" />
  100.         30.00</td>
  101.  
  102.     <td><input name="price0" id="pr5" type="radio" value="37.00" 
  103.  
  104. onclick="total(this.form,10)" />
  105.         37.00</td>
  106.     <td><input name="price0" id="pr6" type="radio" value="42.00" 
  107.  
  108. onclick="total(this.form,10)" />
  109.         42.00</td>
  110.     <td>7.00</td>
  111.     <td><input name="subtotal0" type="text" id="subtotal0" value="0.00" size="5" /></td>
  112.   </tr>
  113.  
  114.  
  115.   <tr>
  116.     <td>Snow blade        </td>
  117.     <td><input name="quantity1" type="text" id="quantity1" value="1.00" size="2" /></td>
  118.     <td><input name="price1" id="pr1" type="radio" onclick="total(this.form,10)" 
  119.  
  120. value="8.50" />
  121.         8.50</td>
  122.     <td><input name="price1" id="pr2" type="radio" onclick="total(this.form,10)" 
  123.  
  124. value="16.00"  />
  125.         16.00</td>
  126.     <td><input name="price1" id="pr3" type="radio" value="23.00" 
  127.  
  128. onclick="total(this.form,10)" />
  129.  
  130.         23.00</td>
  131.     <td><input name="price1" id="pr4" type="radio" value="30.00" 
  132.  
  133. onclick="total(this.form,10)" />
  134.         30.00</td>
  135.     <td><input name="price1" id="pr5" type="radio" value="37.00" 
  136.  
  137. onclick="total(this.form,10)" />
  138.         37.00</td>
  139.     <td><input name="price1" id="pr6" type="radio" value="42.00" 
  140.  
  141. onclick="total(this.form,10)" />
  142.         42.00</td>
  143.  
  144.     <td>7.00</td>
  145.     <td><input name="subtotal1" type="text" id="subtotal1" value="0.00" size="5" /></td>
  146.   </tr>
  147.  
  148.   <tr>
  149.     <td>Superior carving ski        </td>
  150.     <td><input name="quantity2" type="text" id="quantity2" value="1.00" size="2" /></td>
  151.     <td><input name="price2" id="pr1" type="radio" onclick="total(this.form,10)" 
  152.  
  153. value="14.00" />
  154.         14.00</td>
  155.  
  156.     <td><input name="price2" id="pr2" type="radio" onclick="total(this.form,10)" 
  157.  
  158. value="27.00"  />
  159.         27.00</td>
  160.     <td><input name="price2" id="pr3" type="radio" value="36.50" 
  161.  
  162. onclick="total(this.form,10)" />
  163.         36.50</td>
  164.     <td><input name="price2" id="pr4" type="radio" value="46.50" 
  165.  
  166. onclick="total(this.form,10)" />
  167.         46.50</td>
  168.     <td><input name="price2" id="pr5" type="radio" value="55.00" 
  169.  
  170. onclick="total(this.form,10)" />
  171.  
  172.         55.00</td>
  173.     <td><input name="price2" id="pr6" type="radio" value="63.00" 
  174.  
  175. onclick="total(this.form,10)" />
  176.         63.00</td>
  177.     <td>10.50</td>
  178.     <td><input name="subtotal2" type="text" id="subtotal2" value="0.00" size="5" /></td>
  179.   </tr></table></form>
  180.  
In order for the radio buttons to work I name the buttons in their groups (6 buttons) the

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.
Oct 26 '06 #1
1 6632
iam_clint
1,208 Expert 1GB
you can give each one a unique ID and request it by document.getElementById("id")
Oct 26 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Andy | last post: by
10 posts views Thread by Marshall Dudley | last post: by
1 post views Thread by number1.email | last post: by
24 posts views Thread by firstcustomer | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.