Could someone give me a hand? I need to do a sum and subtraction of checkbox and select with values and show the result as the fields are selected, could someone give me a way to go?
Could someone give me a hand? I need to do a sum and subtraction of checkbox and select with values and show the result as the fields are selected, could someone give me a way to go?
Here is a simple example, since you have not made it very clear what you need.
<select id="opcao_soma_val1" name="soma">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<select>
<input id="opcao_soma_val2" value="1" type="number" step="1" min="1" max="10">
<div id="resultado"><!-- aqui vem o resultado --></div>
<button id="somar">Somar</button>
<button id="subtrair">Subtrair</button>
<script
src="http://code.jquery.com/jquery-3.3.1.min.js"integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
<script>
$(function(){
$(document).on('click','#somar', function(){
somar();
});
$(document).on('click','#subtrair', function(){
subtrair();
});
});
function subtrair(){
var val1 = $('#opcao_soma_val1').val();
var val2 = $('#opcao_soma_val2').val();
var result = (parseInt(val1) - parseInt(val2));
$('#resultado').text(result);
}
function somar(){
var val1 = $('#opcao_soma_val1').val();
var val2 = $('#opcao_soma_val2').val();
var result = (parseInt(val1) + parseInt(val2));
$('#resultado').text(result);
}
</script>
To sum in real time, simply blur the last field.
Example:
$('#opcao_soma_val2').on('blur', function(){
somar();
});