0
This is my form:
<form name="ff">
<select id="type" name="type">
<option value="0">Federal</option>
<option value="1">Estadual</option>
<option value="2">Municipal</option>
</select><br>
<div id="state" >
<label>Estado</label>
<input type="text" name="state"/> <br>
</div>
<div id="city">
<label>Cidade</label>
<input type="text" name="city" /><br>
</div>
<label>Nome</label>
<input type="text" name="name" id="name" />
</form>
The jquery script:
$(document).ready(function() {
$('#state').hide();
$('#city').hide();
$('#type').change(function() {
if ($('#type').val() == 1) {
$('#state').show();
$('#city').hide();
}if($('#type').val() == 2) {
$('#state').show();
$('#city').show();
}if($('#type').val() == 0) {
$('#state').hide();
$('#city').hide();
}
});
});
Example: https://jsfiddle.net/73kspqph/5/
But when I edit it is not working properly When I select type the second option to div city does not disable
Example of the problem: https://jsfiddle.net/73kspqph/8
is getting like this
and it should stay that way
The second option is
Estadual
worthwhile1
which according to jQuery shows the state and hides the city. That’s precisely what I see happening in jsfiddle. It is best to try to clarify what is the name of the option you are choosing and what is appearing that should not– Isac
I edited my question with some images to better understand
– Jean Prado
Your Fiddle is already getting as you’d like it to be. What a mistake?
– Sam
the problem is when I will edit this record that already comes selected type: Federal so when it will switch to the Edit page, you will have to be with the hidden city and state input, but it does not stay. as shown in the image example
– Jean Prado
But that’s not what happens in the fiddle you put in. That’s when you switch to
Estadual
the city disappears. Try to put a fiddle that can show the problem– Isac
https://jsfiddle.net/73kspqph/8/ it should be showing the status input
– Jean Prado
You got something I sent @Isac
– Jean Prado
But it appears when you select. It simply does not appear when you open because the code is not executed at the beginning, but when it changes. You can force a call to
change
as soon as the page opens with$("#type").change();
before the end ofdocument
ready
– Isac
Gave straight @Isac, very grateful.
– Jean Prado
By the way you can slightly simplify your logic, so for example
– Isac
that’s what I did!
– Jean Prado