Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>HTML:</p> <pre><code>&lt;form&gt; &lt;fieldset&gt; &lt;p&gt; &lt;label for="selectType"&gt;Type:&lt;/label&gt; &lt;select id="selectType" name="type"&gt; &lt;option value="Hotel" data-cost="40"&gt;Hotel&lt;/option&gt; &lt;option value="Restaurant" data-cost="80"&gt;Restaurant&lt;/option&gt; &lt;option value="Casino" data-cost="35"&gt;Casino&lt;/option&gt; &lt;/select&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="selectLevel"&gt;Level:&lt;/label&gt; &lt;select id="selectLevel" name="level"&gt; &lt;option value="1" data-cost="0"&gt;1&lt;/option&gt; &lt;option value="2" data-cost="15"&gt;2&lt;/option&gt; &lt;option value="3" data-cost="30"&gt;3&lt;/option&gt; &lt;/select&gt; &lt;/p&gt; &lt;p&gt; Total Cost: $&lt;span id="cost"&gt;0&lt;/span&gt; &lt;/p&gt; &lt;p&gt;&lt;input type="submit" value="Go!"&gt;&lt;/p&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>jQuery:</p> <pre><code>$(function() { $("#selectType, #selectLevel").change(function() { var type = $("#selectType"), level = $("#selectLevel"), cost = $("#cost"), typeCost = type.find(":selected").data('cost'), levelCost = level.find(":selected").data('cost'), total = typeCost + levelCost; cost.html(total); }).change(); // invoke the .change() trigger }); </code></pre> <p><a href="http://jsfiddle.net/72p58/1/" rel="nofollow" title="See it in action here">See it in action here</a></p> <p>You might want to also set something like "data-multiplier" for the level... whereas level 2 may have a 1.5x multiplier, and level 3 may have a 2x multiplier. You'd have to adjust the jQuery accordingly as well (<code>total = typeCost * levelCostMultiplier</code>, for example).</p>
 

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