city state Jquery error in selection

Asked

Viewed 235 times

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?

  • 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.

  • tries to bind the CITY CHANGE only after the callback by choosing STATUS $('#city'). load('_requires/cities.php? status=' + value );

  • http://www.dinamicaimoveis.com.br/novo/buscaAvancada.php

  • 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.

  • i inspected the element and noticed that the option="" has disappeared from the select options. Only the cities options are left even

Show 1 more comment

1 answer

2

The problem is occurring because Voce is not returning together with the results of the cities the option Choose the City. This way the first city comes by default as selected but without activating the combo change.

Another way to solve the problem is to change the javascript combo code #Status to:

$(document).delegate('#estado', 'change', function() {
    var valor = $('#estado').val();
    $('#cidade').load('_requires/cidades.php?estado=' + valor, function() {
       $('#cidade').trigger('change');
   });

 });

Note that Trigger will invoke the change event in the #city combo.

  • I think that solves.

  • when I make $('#city'). Trigger('change');, the neighborhood combo is automatically populated but it seems to send empty value to php as a city parameter and then returns without neighborhoods. But even so, when manually selecting the city then the query is done normally

  • Rigger didn’t work so, because before the city values returned, he invoked the function that populates the neighborhood. To work would have to put Trigger inside a timeout or else inside a Callback. But selecting manually will solve. I checked that when changing the state the neighborhood combo remains populated. Adds a code to clear the neighborhood combo too

  • Yes, by testing here, it still doesn’t let you select the first option the same way. See: http://www.dinamicaimoveis.com.br/novo/buscaAvancada.php

  • What it would be like to put Rigger inside a timeout or inside a Callback?

  • You didn’t add the Select Your City option. Add this so I can test.

  • If you call the URL do not bring services @Carlos Rocha http://www.dinamicaimoveis.com.br/new/_requires/cidades.php? estado=SP

  • I updated my answer by putting Trigger inside the callback. Try this way.

  • Still being developed, only have cities registered in MG http://www.dinamicaimoveis.com.br/new/_requires/cidades.php? estado=MG

  • make the adjustments I told you and release there on that link for us to test.

  • ja was. Updated

  • Here, not to bother, but already bothering. I have another question, and as much as I research it, I can’t solve it. Is there any way that you can help me by doing me a favor? http://answall.com/questions/124948/como-definir-focus-no-jhtmlarea

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.