Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you add radio button valus together, yet differentiate them if they have the same value?
    primarykey
    data
    text
    <p>So I'm working on an order form, where people can select different quantities of items, and it then uses JS to calculate to the totals. So for example you can select 2 Brown hats ($15 each) and the TOTAL automatically becomes $30. In the POST page I can then pull the name of the text box and know they ordered 2 brown hats.</p> <p>Here is my problem: I want to set up a radio button option that will allow them to select one item only, and have it add to that final total here is what it looks like:</p> <pre><code>&lt;input type="radio" value="0" name="Syllabus" checked="No" /&gt; Paper ($0)&lt;br /&gt; &lt;input type="radio" value="0" name="Syllabus" checked="No" /&gt; USB ($0)&lt;br /&gt; &lt;input type="radio" value="20" name="Syllabus" checked="No" /&gt; Both ($20)&lt;br /&gt; </code></pre> <p>So how can I use JS to add $20 to the total if "BOTH" is selected? And then of course $20 is removed if they select one of the other options. And finally, if they do select PAPER or USB, how do I know in the final output what they selected, since the value will be 0 either way?</p> <p>Thanks for the help!</p> <p>------ IF IT HELPS ANYONE HERE IS THE CODE I USE TO ADD THE VALUES OF THE OTHER TEXT BOXES, I THEN PAD THE TOTAL WITH ZEROS AND ROUND IT ------</p> <pre><code>// JavaScript Document function CalculateTotal(frm) { var order_total = 0 var syllabuscost; var radios = document.getElementsByTagName('input'); var radiovalue; for (var z = 0; z &lt; radios.length; z++) { if (radios[z].type === 'radio' &amp;&amp; radios[z].checked) { // get value, set checked flag or do whatever you need to radiovalue = radios[z].value; //Change the syllabus cost depending on the value of the radio button if (radiovalue == "Paper" || radiovalue == "USB") { syllabuscost = 0 } else if (radiovalue == "Both") { syllabuscost = 20 } } } // Run through all the form fields for (var i=0; i &lt; frm.elements.length; ++i) { // Get the current field form_field = frm.elements[i] // Get the field's name form_name = form_field.name // Is it a "product" field? if (form_name.substring(0,4) == "PROD") { // If so, extract the price from the name item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1)) // Get the quantity item_quantity = parseInt(form_field.value) // Update the order total if (item_quantity &gt;= 0) { order_total += (item_quantity * item_price) } } } // Display the total rounded to two decimal places Also added the syllabuscost to the total price! frm.TOTAL.value = round_decimals(order_total+syllabuscost, 2) } } function round_decimals(original_number, decimals) { var result1 = original_number * Math.pow(10, decimals) var result2 = Math.round(result1) var result3 = result2 / Math.pow(10, decimals) return pad_with_zeros(result3, decimals) } function pad_with_zeros(rounded_value, decimal_places) { // Convert the number to a string var value_string = rounded_value.toString() // Locate the decimal point var decimal_location = value_string.indexOf(".") // Is there a decimal point? if (decimal_location == -1) { // If no, then all decimal places will be padded with 0s decimal_part_length = 0 // If decimal_places is greater than zero, tack on a decimal point value_string += decimal_places &gt; 0 ? "." : "" } else { // If yes, then only the extra decimal places will be padded with 0s decimal_part_length = value_string.length - decimal_location - 1 } // Calculate the number of decimal places that need to be padded with 0s var pad_total = decimal_places - decimal_part_length if (pad_total &gt; 0) { // Pad the string with 0s for (var counter = 1; counter &lt;= pad_total; counter++) value_string += "0" } return value_string } </code></pre> <p>AND THE HTML</p> <pre><code>&lt;form&gt; &lt;tr&gt; &lt;th&gt;&lt;label for="PROD_BrownShoe_175"&gt;Brown Shoe&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input type="text" name="PROD_BrownShoe_175" onchange="CalculateTotal(this.form)" /&gt; ($175) - Each&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label for="PROD_BlackShoe_255"&gt;Black Shoe&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input type="text" name="PROD_BlackShoe_255" onchange="CalculateTotal(this.form)" /&gt; ($255) - Each&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label for="PROD_BlueShoe_325"&gt;Blue Shoe&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;input type="text" name="PROD_BlueShoe_325" onchange="CalculateTotal(this.form)" /&gt; ($325) - Each&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;Total $:&lt;/th&gt; &lt;td&gt;&lt;input type="text" name="TOTAL" onFocus="this.form.elements[0].focus()" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload