Sum inputs checkbox and at the end subtract one

Asked

Viewed 226 times

0

I rephrased that question, I’ll try to be more objective. I have that field on the form: inserir a descrição da imagem aqui

Where the area code you calculate is this:

  $( "div#molhos :checkbox:checked" ).each(function() {
            sub += $( this ).val() * $( this ).parent().find("select").val();

}); // each molhos

So far so good, what I need is that at the end of the calculation it subtracts ONE, example: If the user chooses THREE SAUCE 1 and TWO SAUCE 3 the total will be 5 sauces, correct? It would be R $ 7,50, but I want the result always stay with LESS ONE, would be the right amount R $ 6,00.

The Rule would be, choosing ONE sauce only, comes out for free, from the second we will charge.

I don’t know if it helps, but there’s more code information: It gets me to the top:

var str = "";
var id = "";
var sub = 0.00;
var taxa = 0;
var taxadeentrega = total - taxa;
var total = 0.00;
var qcount = 0;
var scount = 0;
var opc = 0;
var options = "";
for (i=1; i<21; i++){
    options += "<option value='" + i + "'>" + i + "</option>";
}

This is another area of code that is mentioned the Sauce:

<!--MOLHOS-->

        $( "div#molhos :checkbox:checked" ).each(function() {
              var sel = $(this).parent().children("select").val();
              str = str + $(this).parent().children("strong").text() + ": R$ " + $(this).val().replace(".", ",") + " x " + sel + " = R$ " + (($(this).val() * sel).toFixed(2)).replace(".", ",") + "<br>";
        })
        $('input[name=molhos]').val(str);
        str = "";

But I believe that the important part to create this rule is the first of this post! I believe it is the Rule if, but I can not do, tried everything!!

3 answers

1

So I understand your difficulty is in making the total calculation of the sauces, so it follows the code, basically from the last part.

var totalMolhos = 0;
var gratisQtd = 1;
var valorMolhoUnidade = 1.50;
var valorTotalMolhos = 0;
$( "div#molhos :checkbox:checked" ).each(function(i) {
    $totalMolhos = $( this ).val() * $('campo_com_a_quantidade_do_molho').val();
}); // each molhos

if(totalMolhos>0){
    /*aqui é so subtrair do total a quantidade de molhos gratis, no seu caso 1*/
    valorTotalMolhos = (totalMolhos-gratisQtd) * valorMolhoUnidade;
}
  • look at the code of another field I have in the form, which works with select. This does not have the option for free! $( "div#sobremesas :checkbox:checked" ).each(function() {&#xA; var sel = $(this).parent().children("select").val();&#xA; str = str + $(this).parent().children("strong").text() + ": R$ " + $(this).val().replace(".", ",") + " x " + sel + " = R$ " + (($(this).val() * sel).toFixed(2)).replace(".", ",") + "<br>";&#xA; })&#xA; $('input[name=sobremesas]').val(str);&#xA; str = "";

  • The part that makes the calculation: $( "div#sobremesas :checkbox:checked" ).each(function() {&#xA; sub += $( this ).val() * $( this ).parent().children( "select" ).val();&#xA; }) //each sobremesas

  • I could adapt this from Dessert to Sauce, just do not know where I put the option 1 is free. Ie EQUAL to a choice will be 0 of charge

  • What I realized of your problem is this, Oce just needs to know how many sauces were selected, after that, Oce subtracts how many free sauces Oce this giving for free, the rest is what the customer will have to pay. What I did inside the each() was exactly calculate the total of sauces that the user selected.

1

I imagine that solves your problem:

$('#molhos input[type="checkbox"]').change(function() {

    var count = $('#molhos input[type="checkbox"]:checked').length;
    count = count == 0 ? 0 : count-1;
  
    var result = 1.50*count;
  
    $('#total').html('Total a pagar nos molhos R$ ' + result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="molhos">
  
  <div>
    <input type="checkbox" value="1"> Molho 1: R$ 1,50
  </div>
  
  <div>
    <input type="checkbox" value="2"> Molho 2 R$ 1,50
  </div>
  
  <div>
    <input type="checkbox" value="3"> Molho 3 R$ 1,50
  </div>
  
  <div>
    <input type="checkbox" value="4"> Molho 4 R$ 1,50
  </div>
  
</div>

<div id="total"></div>

Every time a checkbox is changed i run the function to calculate the number of selected sauces and display the message pro user accordingly, but remember that it is important to perform an additional check on the server side!

  • How to do to get the higher value ?

  • @Rafa You can use Math.max combined with the Perator spread to get the maximum value contained within an array, it would look like this: Math.max(...arr). Take a look here

0

When you give a Get at the value the user entered on the screen, do a check... if the entered value is > 1 you charge for the informed quantity -1.

Ex:

if($_POST['qtdeMolhos'] > 1){
    $valor = 1.5*($_POST['qtdeMolhos']-1);
}else{
    $valor = 0;
}

Did you understand? If it informs 1 the value will be R $ 0,00 and any value greater than this, it multiplies the base value R $ 1,50 by the amount informed less the amount that the house has as courtesy.

  • Don’t interfere with something else I already have at the top of that file the following code: var str = "";&#xA;var id = "";&#xA;var sub = 0.00;&#xA;var taxa = 0;&#xA;var taxadeentrega = total - taxa;&#xA;var total = 0.00;&#xA;var qcount = 0;&#xA;var scount = 0;&#xA;var opc = 0;

  • I believe it doesn’t interfere... you’re just stating and instantiating the variables before. Try implementing the code I passed above to see if it will work. I implemented in PHP because I didn’t know if this will be done in PHP or Javascript.

  • Okay, I’ll try, thanks for your attention!

  • I set up this whole form with the help of this site, I understand little programming, I’m still studying. This would be the code of this area in PHP: <!-- MOLHOS -->&#xA;<div id="molhos">&#xA;<span class="titulo">Escolha o seu Molho!</span><br>&#xA;<span style="font-size: 12px"><strong><em style="font-size: 11px">(Você tem direito a um molho, totalmente grátis) </em></strong></span>&#xA;<br>&#xA;<div style="color: #FFFFFF">espaço</div>&#xA;<div><input type="checkbox" value="1.50"> <strong>MOLHO 1</strong><span>: R$ 1,50 - Quant.: <select></select></span></div>&#xA;

  • Look at the code of another field I have in the form, which works with select. This does not have the option for free! $( "div#sobremesas :checkbox:checked" ).each(function() { var sel = $(this).parent().children("select").val(); str = str + $(this).parent().children("strong").text() + ": R$ " + $(this).val().replace(".", ",") + " x " + sel + " = R$ " + (($(this).val() * sel).toFixed(2)).replace(".", ",") + "<br>"; }) $('input[name=sobremesas]').val(str); str = ""; I could adapt this to the sauce, with the option of 1 free. It has as?

  • The part that makes the calculation: $( "div#sobremesas :checkbox:checked" ).each(function() {&#xA; sub += $( this ).val() * $( this ).parent().children( "select" ).val();&#xA; }) //each sobremesas

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.