Data jQuery problems

Asked

Viewed 35 times

1

I’m having a little problem in my jquery code, I wanted to make a sum in which I put the value in the input + the span value in a div and generate it in another span, but it does not show me results.

HTML

<div class="media-body">
<div class="menu-tittle">

</div>
<div class="quantity">
  <form action="#">
    <div class="pizza-add-sub">
      <input type="text"  class="qtdpedidos" />
    </div>
  </form>
</div>
<div class="pizza-price"> <span class="pizza">10.00</span>
</div>

Order Value: R$0.00

JQUERY

 $("#somar").click(function(){
  var total = 0;
  var valor= $('.pizza').val());

  $('.qtdpedidos').each(function(){
    var valor = Number($(this).val());
    if (!isNaN(valor)) total += valor;
  });

  total = total - desconto;
  $(".resultado").html(total.toFixed(2));
});

Thank you in advance

I’m testing here https://jsfiddle.net/bx3c5trg/

1 answer

1


Follow a snippet with a alternative solution, note that was giving an error because in your code

var valor= $('.pizza').val());

is with an extra parenthesis, something else is the variable desconto which has not been declared anywhere. I hope it helps.

$("#somar").click(function() {
  var total = 0;
  var valorPizza = $('.pizza').text().substring(0, $('.pizza').text().indexOf('.'));
  //console.log(valorPizza);

  $('.qtdpedidos').each(function() {
    var valorInput = parseInt($(this).val());
    //if (!isNaN(valor)) total += valor;
    total = parseInt(valorPizza) + parseInt(valorInput);
  });

  //total = total - desconto;
  $(".resultado").html(total.toFixed(2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="media-body">
  <div class="menu-tittle">

  </div>
  <div class="quantity">
    <form action="#">
      <div class="pizza-add-sub">
        <input type="text" class="qtdpedidos" />
      </div>
    </form>
  </div>
  <div class="pizza-price"> <span class="pizza">10.00</span>
  </div>
</div>
<input type="button" value="SOMAR" id="somar" /> Valor do Pedido: R$<span class="resultado">0.00</span>

  • Solved yes, thank you Aunts!! n

Browser other questions tagged

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