Data jQuery problems

1

I'm having a small problem in my jquery code, I wanted to do a sum in which I put the value in the input + the span value in one div and generate that in another span, but it does not show me results. p>

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 Amount: $ 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 link

    
asked by anonymous 27.04.2017 / 15:24

1 answer

1

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

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

It has one more parentheses, another thing is the desconto variable that has not been declared anywhere. 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><divclass="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>
    
27.04.2017 / 15:36