0
I’m using Materialize CSS, I have a screen where I would like the Switch to be activated, the select
the nearest becomes active so the user can select an option, and if the Switch goes back to inactive the select
also return to idle state.
HTML
<form id="relatorio" name="relatorio" class="col s12">
<div class="row">
<div class="input-field col s4 l4">
<select id="filtro2" name="filtro2" disabled>
<option value="" disabled selected>Filtro</option>
<option value="1">Opção 1</option>
<option value="2">Opção 3</option>
<option value="3">Opção 3</option>
</select>
<label>Filtro</label>
</div>
<div class="switch col s2 l2">
<div class="switch">
<label>
Não
<input type="checkbox">
<span class="lever"></span>
Sim
</label>
</div>
</div>
<div class="input-field col s4 l4">
<select id="filtro" name="filtro" disabled>
<option value="" disabled selected>Filtro</option>
<option value="1">Opção 1</option>
<option value="2">Opção 3</option>
<option value="3">Opção 3</option>
</select>
<label>Filtro</label>
</div>
<div class="switch col s2 l2">
<div class="switch">
<label>
Não
<input type="checkbox">
<span class="lever"></span>
Sim
</label>
</div>
</div>
</div><!-- /row -->
</form>
JS
$(document).ready(function () {
$(".switch").find("input[type=checkbox]").on("change",function() {
var filtro = $(this).closest("input[type=checkbox]").prop('checked');
if(filtro == true) {
alert("true");
//$(this).closest('select').prop('disabled', true);
} else {
alert("false");
//$(this).closest('select').prop('disabled', false);
}
});
});
I can already get the Switch value to know when to enable select
and when to disable it, but I can’t find which the select
nearest to change his state.
Are several selects
I need to do this, so finding the one closest to the Switch and swapping out its value is essential.
Thank you in advance.
I can’t understand why my code didn’t work, now it seems to be getting the correct select, except if there are 2 in it
<div class="row">
, then he selects the two, and select has not activated in either case, I tried to change.prop
for.atrr
, but I did not succeed.– Dobrychtop
@Dobrychtop Never use
.attr
to change true or false values because it doesn’t work... Well, show me an example of how it would be with 2 selects to see how it would look.– Sam
I solved the problem of not selecting, this was caused by Materialize and how he makes the
selects
, destroyed them before switching property and booted, now I have the problem of selecting the two in the samerow
– Dobrychtop
I edited the question with 2
selects
in the samerow
– Dobrychtop
@Dobrychtop modified answer.
– Sam
It worked perfectly, thank you for your attention.
– Dobrychtop
@Dobrychtop Dispo! Still put a tip at the end of the answer. Just update. Abs!
– Sam