1
I have a calculation of a input
with a working variable. I have two buttons to increase and decrease the value of input
, but when I use the buttons the calculation is not done in real time, it is only done when I click for the second time on it and still the calculation is done with the previous value that was in the input
, and not updated as it should.
//botões - e +
$( document ).on( 'click', '.box-right-delivery button', function () {
var btn = $( this ),
oldValue = btn.closest( '.box-right-delivery' ).find( 'input' ).val().trim(),
newVal = 0;
if ( btn.attr( 'data-dir' ) == 'up' ) {
newVal = parseInt( oldValue ) + 1;
} else {
if ( oldValue > 1 ) {
newVal = parseInt( oldValue ) - 1;
} else {
newVal = 0;
}
}
btn.closest( '.box-right-delivery' ).find( 'input' ).val( newVal );
} );
//função calcular valor
function Calcular() {
var valor1 = 3.35;
var campo1 = Number( document.getElementById( "campo1" ).value );
var result = document.getElementById( "Resultado" );
if ( result.textContent === undefined ) {
result.textContent = ( campo1 * valor1 ).toFixed( 2 );
} else { // IE
result.innerText = ( campo1 * valor1 ).toFixed( 2 );
}
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="box-right-delivery">
<input type="text" class="form-control input-sm center merge-bottom-input" name="campo1" id="campo1" onchange="Calcular(this.value)" value="0">
<div class="box-button-right-delivery" role="group" aria-label="plus-minus">
<button type="button" class="minus-button merge-top-left-button btn-circle" data-dir="dwn" id="dwn" onClick="Calcular(this.value)">
<span class="glyphicon glyphicon-minus"></span>
</button>
<button type="button" class="plus-button merge-top-right-button btn-circle" data-dir="up" id="up" onClick="Calcular(this.value)">
<span class="glyphicon glyphicon-plus"></span>
</button>
</div>
</div>
<div id="Resultado"></div>
When you click the first time, say "+", what is the expected value?
– Sam
The first is 3.35, in the second *2 and so on. if I type straight into the input is working perfectly.
– Hugo Nascimento