2
Good in the example I posted I can disable the input valor1
, vendedor1
and supervisor1
by checking the option recalcular1
.
The problem is that I have a form with several input, and to avoid a very large code I wanted to make the jqeury identify the number of the checkbox
and disable only inputs from the same number.
Example:
The recalcular1
disable from the fields valor1
, vendedor1
and supervisor1
.
The recalcular2
disable from the fields valor2
, vendedor2
and supervisor2
.
Does anyone know a practical way to do this?
$(document).ready(function() {
$("#recalcular1").on("blur change", function() {
// Verifica se exibe
if ($("#recalcular1").is(':checked')) {
$("#valor1").prop('disabled', true);
$("#vendedor1").prop('disabled', true);
$("#supervisor1").prop('disabled', true);
} else {
$("#valor1").prop('disabled', false);
$("#vendedor1").prop('disabled', false);
$("#supervisor1").prop('disabled', false);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' id='recalcular1' class='checkbox' name='recalcular1'>
<div>
<input type='text' id='valor1' name='valor1'>
<input type='text' id='vendedor1' name='vendedor1'>
<input type='text' id='supervisor1' name='supervisor1'>
<!-- Não pode ficar desabilitado -->
<input type='text' id='qtd1' name='qtd1'>
</div>
<br><br>
<input type='checkbox' id='recalcular2' class='checkbox' name='recalcular2'>
<div>
<input type='text' id='valor2' name='valor2'>
<input type='text' id='vendedor2' name='vendedor2'>
<input type='text' id='supervisor2' name='supervisor2'>
<!-- Não pode ficar desabilitado -->
<input type='text' id='qtd2' name='qtd2'>
</div>
Your solution was legal, but mine
checkbox
doesn’t stay the samediv
that theinput
. That’s one thing I can’t change.– Hugo Borges
@Hugoborges posts the structure of your html, to see if we can help.
– abfurlan
Okay, I edited the answer with an example of my html structure.
– Hugo Borges
@Hugoborges see my issue meets, you will have to check the next
div
, find all inputs within it excluding the quantity, so you will need a class in this field.– abfurlan
It was good, but this way I get tied to the layout, is there any way to do it using an attribute? Like this:
var inputValor = $(this).attr("valor"); 
 $("#" + inputValor).prop('disabled', true);
– Hugo Borges
@Hugoborges you can also do so https://jsfiddle.net/8qz86skh/
– abfurlan
That way it got really good, thanks. it worked out here
– Hugo Borges