Error updating values fields with jquery;

Asked

Viewed 59 times

0

I am having trouble updating the net value field of my page. I have the following fields in my HTML:

<input type="number" id="total" value="<?=$faturamento['valor_total'];?>" readonly name="valor" class="form-control">

<input type="number" class="form-control" id="frete" onchange="atualizaTotalLiquido()">

<input type="number" class="form-control" id="desconto" onchange="atualizaTotalLiquido()">

<input type="text" class="form-control" id="valor_liquido" value="<?=$faturamento['valor_total'];?>">

And this is my jquery;

function atualizaTotalLiquido(){
  var frete = $("#frete").val();
  var desconto = $("#desconto").val();
  var total = $("#total").val();
  var valor_liquido = $("#valor_liquido");
  var total_liquido = total+frete-desconto;
  valor_liquido.text(total_liquido);
}

But the total liquid field does not change.


Solution:

$(document).ready(function () {

    $('#frete').change(function(){
        atualizaTotalLiquido();
    });

    $('#desconto').change(function(){
        atualizaTotalLiquido();
    });

    function atualizaTotalLiquido(){

        var valor_liquido = 0;

        //var total_liquido = total+frete-desconto;

        valor_liquido = Number($("#total").val()) + Number($("#frete").val()) - Number($("#desconto").val());

        $('#valor_liquido').val(valor_liquido).toFixed(2);
    }
});
  • Change .text for .val.

  • Even so it doesn’t work. And I tried to log the fields I search and the values come, but it doesn’t work.

  • It did work. I will edit my question with the solution.

1 answer

2


@Bruno you are in the way, but there are some passages that are not correct.

Your example:

$(document).ready(function () {

	$('#frete').change(function(){
		atualizaTotalLiquido();
	});
	
	$('#desconto').change(function(){
		atualizaTotalLiquido();
	});

	function atualizaTotalLiquido(){
		
		var valor_liquido = 0;
	  	  
		//var total_liquido = total+frete-desconto;
		
		valor_liquido = Number($("#total").val()) + Number($("#frete").val()) - Number($("#desconto").val());
	  
		$('#valor_liquido').val(valor_liquido);
	}
});
<html>
	<body>
		<form class='form-control'>
			<!-- Valor de acordo com a variável que vem do banco ou de outro processo-->
			
			<label for="valor">Total</label> <br>
			<input type="number" id="total" readonly name="valor" class="form-control" value='5' > <br>

			<label for="frete">Frete</label> <br>
			<input type="number" class="form-control" id="frete"> <br>

			<label for="desconto">Desconto</label> <br>
			<input type="number" class="form-control" id="desconto"> <br>

			<label for="valor_liquido">Valor liquido</label> <br>
			<input type="text" class="form-control" id="valor_liquido"> <br>

			<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
			<script type="text/javascript" src="master.js"></script>
		</form>
	</body>
</html>

I recommend taking a look at the basics again.

HTML - JS - jQuery

Browser other questions tagged

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