javascript - How to address to values of the two INPUTs of one position, if there are many positions -


i'm beginner in jquery, please simplifying following function in java script (by converting jquery). need write price calculator of goods.

a piece html code

<li>     <input type="checkbox" class="option" value="5000" id="price-cb-1" onchange="calculate_price();">     <input id="price-uts-1" class="price-count" type="number" min="1" onchange="calculate_price();" value="1"> </li> <li>     <input type="checkbox" class="option" value="9000" id="price-cb-1" onchange="calculate_price();">     <input id="price-uts-1" class="price-count" type="number" min="1" onchange="calculate_price();" value="1"> </li> 

.....there 30 elements of

 <li>..</li>  

with different values(prices). need add prices, multiplied quantity, add of it, , total amount. how address values @ same time of 2 inputs using jquery?

java script

var sum = 0;     var prefixcheckbox = "price-cb-";   (var = 1, ids = ($('.price')).length; i<=ids; i++) {      if (document.getelementbyid(prefixcheckbox+i).checked === true){        sum+=(document.getelementbyid(prefixunits+i).value)*         (document.getelementbyid(prefixcheckbox+i).value)       } $('#total').html(sum); 

on change event of both types of input can loop on each checked element, multiplying value quantity. try this:

$('.option, .price-count').change(function() {     var sum = 0;               $('.option:checked').each(function() {         sum += $(this).val() * $(this).next('.price-count').val();     });      $('#total').html(sum); }).change(); 

example fiddle


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -