Add checkbox inputs and in the end subtract one

0

I have rephrased this question, I will try to be more objective. I have this field in the form:

Wherethecodeareathatcomputesisthis:

$("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? I would give $ 7.50, but I want the result to always be with LESS ONE, it would be the right amount $ 6.00.

The rule would be to choose ONE sauce, it will be free, from the second one we will charge.

I do not know if it helps, but it goes further information from the code: This is 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 the code that Sauce is mentioned:

<!--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 think it's the if rule, but I can not do it, I've tried everything !!

    
asked by anonymous 24.01.2017 / 13:23

3 answers

1

So I've got your difficulty in doing the total calculation of the sauces, so follow the code, basically 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;
}
    
24.01.2017 / 13:33
1

I imagine this will solve 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><divid="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 rotate the function to calculate the number of selected sauces and display the message to the user accordingly, but remember that it is important to perform an additional check on the server side!

    
24.01.2017 / 14:58
0

When you give Get a value that the user entered on the screen, perform a check ... if the value entered is > 1 you charge for the reported amount -1.

Ex:

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

Have you understood? If he informs 1 the value will be $ 0.00 and any amount greater than that, he multiplies the base value R $ 1.50 by the amount informed less the amount that the house disposes as a courtesy.     

24.01.2017 / 13:33