I have a form
, and in this I have to inform the neighborhood, however the element that stores the information of the neighborhood can vary according to the value of the city selected previously.
Example:
If the city is "main" it should change I should change the neighborhood field from input
to select
. I was able to make the exchange like this:
HTML:
<div class="form-group has-feedback {{ $errors->has('bairro') ? 'has-error' : '' }}">
<label for="bairro" class="col-sm-2 control-label">Bairro</label>
<div id="bairro-select" class="col-sm-10" hidden>
<select class="form-control" name="bairro" id="bairro">
<option value="" selected>--- Escolha um bairro ---</option>
@foreach($bairros as $bairro)
<option value="{{ $bairro->nome }}">{{$bairro->nome}}</option>
@endforeach
</select>
</div>
<div id="bairro-input" class="col-sm-10">
<input type="text" class="form-control" name="bairro" id="bairro" placeholder="" minlength="3" maxlength="90">
</div>
</div>
JavaScript:
$("#cidade").on('change', function() {
var cidade = $(this).find('option:selected').val();
if(cidade == 'Principal') {
bairroselect.style.display = "block";
bairroinput.style.display = "none";
}else {
bairroselect.style.display = "none";
bairroinput.style.display = "block";
}
});
But when I send form
to request
, bairro
is always with content input
, even if selecting bairro
in select
this takes the value of input
.
The code I've made leaves the div
just hidden the element. How could I do so that only the value that is displayed is submitted?