Calculate the amount selected in the selects



  • There's a sign, a picture. таблица

    We need to count the real-time service, about 50 on the page.
    That is, for example, the 2nd local 2nd and 3 couch sites have been selected, i.e., the price of the service should be multiplied by its number.



  • Such an option:

    function recalculatePrices() {
      var total = 0;
      $('.service').each(function() {
        total += $(this).val() * $(this).attr('data-price');
      });
      $('#result').html(total);
    }
    

    $(document).ready(function() {
    $('.service').on('change', recalculatePrices);
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select data-price="5" class="service">
    <option></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>
    <br />
    <select data-price="3" class="service">
    <option></option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>
    <div id="result"></div>

    Comments:

    • All lists have to be service♪ It is this class that will then be determined, included or not
    • Each list must have an attribute. data-price♪ This price will be used in the calculations
    • jQuery bind Each list shall bear the event onChangeand it'll start the recounting function. recalculatePrices();

    I hope that suits you.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2