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

254
Visualizações
How to insert price based on checkbox checked

I have a form where I calculate the price based on the checkboxes checked. But I am having trouble as its not updating the final price correctly. I did some jQuery coding but it's not functioning properly. How can I debug the code and fix it?

//Price Calculator
$(document).ready(function(){
  
  function Calculator(){
    let totalAmount = 0;
    if($('input[datasource=service_0]').is(':checked')){  // First Checkbox targeted using datasource attribute
      totalAmount = 395;
      return totalAmount;
    }else if($('input[datasource=service_1]').is(':checked')){   // First Checkbox targeted using datasource attribute
      totalAmount = 392;
      return totalAmount;
    }else if ($("input[datasource=service_0]:checked,input[datasource=service_1]:checked").length == 2) {
      totalAmount = 397;
      return totalAmount;
    }
  }
  //Insert Updated Amount to Budget field
  $("input[type=checkbox]").on("click", function(){
    if ($("input[datasource=service_0]:checked,input[datasource=service_1]:checked").length == 2){
      $("input[name=wpcf-book-amount]").val(Calculator());
    } else{
      $("input[name=wpcf-book-amount]").val(Calculator());
    }
  })
  
})

<div class="extra-services">
    <ul>
        <li>
            <input type="checkbox" name="wpcf-additional-services[]" data-value="5" id="wpcf-fields-checkboxes-option-d41b805f6cbf1d0ab6ee12b8dda8b47d-1" datasource="service_0">
            <label for="wpcf-additional-services">Cot (EUR 5)</label>
        </li>
        <li>
            <input type="checkbox" name="wpcf-additional-services[]" data-value="2" id="wpcf-fields-checkboxes-option-d41b805f6cbf1d0ab6ee12b8dda8b47d-1" datasource="service_1">
            <label for="wpcf-additional-services">BABY CHAIR (EUR 2)</label>
        </li>
    </ul>
    <input type="text" name="wpcf-booking-amount" id="booking_field" placeholder="Booking Amount (System Generated)" readonly>
</div>

Form containing the fields responsible to calculate the booking amount

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

0

<html>
    <head></head>
    <body>
        <input type="checkbox"  id="1st" onclick="ge()"  value=500>
  <label for="1st"> I have a bike</label><br>
  <input type="checkbox"  id="2nd" onclick ="ye()" value=200>
  <label for="2nd"> I have a car</label>

<input type="number" id="here" value="0">
        <script>
        var amt = 0;
        var st = document.getElementById("1st");
        var nd = document.getElementById("2nd");
        var num = document.getElementById("here");
        function ge() {
            
            
            if(st.checked == true ){
                var g =  parseInt(st.value);
            amt = amt + g;
            num.value = amt ;
            } 
            else{
                
                amt = amt - parseInt(st.value);
                num.value = amt;
                
            }
        }
        function ye() {
            if(nd.checked == true){
                var g =  parseInt(nd.value);
            amt = amt + g;
            num.value = amt ;
            }
            else{
                amt = amt - parseInt(nd.value);
                num.value = amt;
            }
        }
        </script>
    </body>
</html>

Here you go if it's only for two checkbox this will work, for live demo click here

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