Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
How to get the default calculation for certain equation after uncheck checkbox using JS

I have two inputs one of them should have the value of Quantity and the other one have the value of Price the first input which is quantity of type number has disabled attribute when check the checkbox the disabled removed then; when changing it; it's calculating the total of price and quantity.

For example: quantity = 2 and price = 10 so the total should equals total=2*10=20

But the problem here when uncheck the checkbox I don't want the new calculation. It should get the default calculation that it saved in the database before including the quantity.

Based in that here is the sample code explaining my issue:

    $(document).on('click', '.check_box', function(){
        var price = $('.price').val();
        if(this.checked){
            $('.quantity').removeAttr('disabled');
            $(document).on('change', '.quantity', function(){
                var quantity = $(this).val();
                total = quantity*price;
                $('#total').text(total.toFixed(2));
            });
        } else {
            $('.quantity').attr('disabled', 'disabled');
            // What should I do in else statement?
        }
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <td>#</td>
      <td>Item name</td>
      <td>Quantity</td>
      <td>Price</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox" class="check_box" /></td>
      <td>Pizza</td>
      <td><input type="number" class="quantity" value="1" disabled/></td>
      <td><input type="text" class="price" value="20.00" disabled /></td>
    </tr>
  </tbody>
</table>
Total: <p id="total">40.00</p> <!-- Let's say this total already saved in database before -->

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

An option is to save the original value from database using data-xxxx and show it when required.

<p id="total" data-original="40.00">40.00</p>

...

$('#total').text($('#total').data('original'));

It's better to move the change event handler out, otherwise it will attach multiple times when tick/untick the checkbox.

$(document).on('click', '.check_box', function(){
    if(this.checked){
        $('.quantity').removeAttr('disabled');
        $('.quantity').trigger('change');
    } else {
        $('.quantity').attr('disabled', 'disabled');
        $('#total').text($('#total').data('original'));
    }
});
$(document).on('change', '.quantity', function(){
    if (!this.disabled) {
        var quantity = $(this).val();
        var price = $('.price').val();
        total = quantity*price;
        $('#total').text(total.toFixed(2));
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <td>#</td>
      <td>Item name</td>
      <td>Quantity</td>
      <td>Price</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox" class="check_box" /></td>
      <td>Pizza</td>
      <td><input type="number" class="quantity" value="1" disabled/></td>
      <td><input type="text" class="price" value="20.00" disabled /></td>
    </tr>
  </tbody>
</table>
Total: <p id="total" data-original="40.00">40.00</p> <!-- Let's say this total already saved in database before -->

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda