1
I’m creating a simple form, in which I have to make a calculation of two fields to appear a message, until then no problem, but since there are several fields that I will have to apply the same formula several times, I wanted to do it in a more dynamic way, not just copy the same script over and over again. Follow my code below
<div class="col-4">
<label for="habitantes" class="label">Número de Habitantes:</label>
<input type="number" name="habitantes" id="habitantes">
</div>
<div class="col-4">
<label for="habitantes2" class="label">Número de Habitantes:</label>
<input type="number" name="habitantes2" id="habitantes2">
</div>
<div class="col-4">
<label for="consultores" class="label">Número de Consultores Cadastrados</label>
<input type="number" name="consultores[]" id="consultores">
<span style="color:green" class="resultado"></span>
</div>
<div class="col-4">
<label for="consultores2" class="label">Número de Consultores Cadastrados</label>
<input type="number" name="consultores[]" id="consultores2">
<span style="color:green" class="resultado2"></span>
</div>
<script>
$('#consultores').change(function(event) {
event.preventDefault();
$('#consultores').each(function() {
var valor = $('#habitantes').val();
var valor2 = $('#consultores').val();
var calculo = valor / valor2;
if(calculo>=100){
$('.resultado').html("deu certo");
}
});
});
$('#consultores2').change(function(event) {
event.preventDefault();
$('#consultores').each(function() {
var valor = $('#habitantes2').val();
var valor2 = $('#consultores2').val();
var calculo = valor / valor2;
if(calculo>=100){
$('.resultado2').html("deu certo");
}
});
});
<script>
I advise you to use a form validation lib I can advise you to use http://formvalidation.io/examples/ see the examples and documentation
– Nuno Maximiano