javascript - Jquery: calculate volume -


took base of code 1 user , modified bit. need - calculates sum 3 values (first 2 in meters, third in centemeters). more simpler. dont't need "select options" in thickness field - it must calculated in centemeters!. , second request - the amount must in m3!

html:

<table>     <tbody>         <tr>             <td>width (m)</td>             <td>                 <input type="text" id="width" />             </td>         </tr>         <tr>             <td>length (m)</td>             <td>                 <input type="text" id="length" />             </td>         </tr>         <tr>             <td>thickness (cm)</td>             <td>                 <input type="text" id="thickness" />             </td>             <td>                 <select id="sel">                     <option>centemeter</option>                     <option>meter</option>                     <option>melemeter</option>                 </select>             </td>         </tr>         <tr>             <td>total (m<sup>3</sup>)</td>             <td id="answer"></td>         </tr>     </tbody> </table> 

javascript:

$("#width ,#length ,#thickness, #sel").on('change keyup keydown', function() {     var width = $("#width").val();     var length = $("#length").val();     var thickness = $("#thickness").val();     var result = width * length * thickness;     var select_val = $("#sel").val();      if (select_val == "centemeter") {         $("#answer").text(result).append(" cm<sup>3</sup>");;     } else if (select_val == "meter") {         result = result / 100;         $("#answer").text(result).append(" m<sup>3</sup>");;     } else if (select_val == "melemeter") {         result = result * 10;         $("#answer").text(result).append(" mm<sup>3</sup>");     } }); 

jsfiddle

update: thought easy task: calculate amount of 3 numbers - var result = width * length * thickness; thickness 1/100 of width , length...

below solution answer. wasn't complicated, it?

$("#width ,#length ,#thickness").on('change keyup keydown', function () {              var width = $("#width").val();             var length = $("#length").val();             var thickness = $("#thickness").val();             var result = width * length * thickness;              result = result/100;             $("#answer").text(result).append(" m<sup>3</sup>");;          }); 

jsfiddle


Comments

Popular posts from this blog

How to use SUM() in MySQL for calculated values -

loops - Spock: How to use test data with @Stepwise -