Jquery data refresh

0

I have a pertinent question about updating dynamically:

I have a ordering system.

The problem is that the values are not being summed correctly in the click event.

InthefirstimageIjustclickthe"+" button and the text box adds the number.

In the second the value of the request receives all the value already added.

HTML

    <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>
<p>Valor do Pedido: R$<span class="resultado">0.00</span></p>

JS

 $(".pizza-add-sub").append('<div class="plus qty-pizza">+</div><div class="mines qty-pizza">-</div>');
$(".qty-pizza").on("click", function() {

var $button = $(this);
var oldValue = $button.parent().find("input").val();
if ($button.text() == "+") {
  var newVal = parseFloat(oldValue) + 1;
} else {
  // Don't allow decrementing below zero
 if (oldValue > 0) {
    var newVal = parseFloat(oldValue) - 1;
    } else {
    newVal = 0;
  }
  }
$button.parent().find("input").val(newVal);
var total = 0;
var valorPizza = $('.pizza').text().substring(0, $('.pizza').text().indexOf('.'));
    
asked by anonymous 27.04.2017 / 22:55

1 answer

0

	$(document).ready(function() {
	 
	    $(".pizza-add-sub").append('<div class="plus qty-pizza">+</div><div class="mines qty-pizza">-</div>');
	    $(".qty-pizza").on("click", function() {
	    
	    var $button = $(this);
	    var oldValue = $button.parent().find("input").val();
		if ($button.text() == "+") {
		  var newVal = parseFloat(oldValue) + 1;
		} else {
		  // Don't allow decrementing below zero
			 if (oldValue > 0) {
			    var newVal = parseFloat(oldValue) - 1;
			    } else {
			    newVal = 0;
			  }
		}
	    $button.parent().find("input").val(newVal);
		var v1 = $('#item1 span').text();
		var v2 = Number(document.getElementById("qtdpedidos").value);
	    document.getElementById("resultado").innerHTML = parseFloat(v1 * v2).toFixed(2);
	
	 });
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><formaction="#">
<div class="media-body">
  <div class="menu-tittle">
</div>
  <div class="quantity">
    
      <div class="pizza-add-sub">
        <input type="text" id="qtdpedidos" class="qtdpedidos" value="0"/>
      </div>
    </form>
  </div>
  <div id="item1" class="pizza-price"> <span id="pizza" class="pizza">10.00</span>
  </div>
</div>
</form>
<p>Valor do Pedido: R$<span id="resultado" class="resultado">0.00</span></p>
    
28.04.2017 / 01:03