4
Jquery:
$(function() {
$(document).delegate('#estado', 'change', function() {
var valor = $('#estado').val();
$('#cidade').load('_requires/cidades.php?estado=' + valor );
});
$(document).delegate('#cidade', 'change', function() {
var valor = $('#cidade').val();
alert(valor);
$('#bairro').load('_requires/bairros.php?cidade=' + valor );
});
});
HTML:
<div>
<label class="labelPequena">Estado</label><br />
<select class="typeTextMedio" id="estado" name="estado" required>
<option value="">Escolha o Estado</option>
<option value="MG">MG</option>
<option value="ES">ES</option>
<option value="RJ">RJ</option>
<option value="SP">SP</option>
</select>
</div>
<div>
<label class="labelPequena">Cidade</label><br />
<select class="typeTextMedio" id="cidade" name="cidade" required>
<option value="">Escolha a Cidade</option>
</select>
</div>
<div>
<label class="labelPequena">Bairro</label><br />
<select class="typeTextMedio" id="bairro" name="bairro" required>
<option value="#">Indiferente</option>
</select>
</div>
When selecting in the combo estados
, popular the combo cidades
. This is working.
When selecting in the combo cidades
, popular the combo bairro
. This is NOT working. But I noticed that in combo cidades
only occurs the ERROR with the first city only and when I click on it, the event does not occur. With the others usually occurs.
An important observation is that if choosing second city of combo
, and the combo
of the neighborhoods is loaded, then go back and select the first city again, then now it works. On the first try nor the alert
works!
How to solve?
The PHP part is receiving and returning the data correctly?
– Fleuquer Lima
Yes. Everything normal, the combos fill normally. The problem is that when selecting a state the first city option does not trigger the event to popular combo neighborhoods. But from the second city on it works. And, after the next town, if it comes back on the first, then now it works.
– Carlos Rocha
tries to bind the CITY CHANGE only after the callback by choosing STATUS $('#city'). load('_requires/cities.php? status=' + value );
– PauloHDSousa
http://www.dinamicaimoveis.com.br/novo/buscaAvancada.php
– Carlos Rocha
the city combo is initially loaded with <option value="">Choose City</option>. - check that when you bring cities back from the selected state, this option is being returned along with the results. If not, then do it.
– Euler01
i inspected the element and noticed that the option="" has disappeared from the select options. Only the cities options are left even
– Carlos Rocha