tags:

views:

508

answers:

3

I have a 3 dropdowns: countries, states, and cities.

States and cities are dependent of the previous form and loaded via AJAX.

In Firefox and Chrome everything is ok, but in IE (8 in my case) when I select a state, the cities dropdown isn't loaded. It is like IE don't detect the change. It is not a load problem, because I have tested with a simple alert box.

Any help will be really appreciated.

The states loaded page is similar to:

<select id="woeid_state">
<option value="1">Alabama</option>
<option value="2">Florida</option>
</select>

The cities loaded page is similar to:

<select id="woeid_town">
<option value="100">Miami</option>
<option value="101">Orlando</option>
</select>

The JS

$(document).ready(function()
 {
    function loadStates( parent_woeid )
    {
        $('#states').load("<?php echo $states_page?>"+parent_woeid);
        return false;           
    }

    function loadCities( parent_woeid )
    {
        $('#towns').load("<?php echo $cities_page;?>/admin1/"+parent_woeid);
        return false;           
    }   

    $("#woeid_country").change(function()
    {
        //alert("I am an alert box");
        var country = $("select#woeid_country").val();
        loadStates ( country);
    });

    $("#states").change(function()
    {
        //alert("I am an alert box");

        var admin1 = $("select#woeid_state").val();
        loadCities ( admin1 );
    }); 

});     

The form:

            <form class="ordinary_form" method="post" action="">
                <label>Country</label>

                <select name="woeid_country" id="woeid_country">
                                        <option value="23424975">United Kingdom</option>
                                        <option value="23424977">United States</option>
                                        <option value="23424979">Uruguay</option>
                                        <option value="23424980">Uzbekistan</option>
                                        <option value="23424907">Vanuatu</option>
                                        <option value="23424982">Venezuela</option>
                                        <option value="23424984">Vietnam</option>
                                    </select>

                <label>State/Province</label>
                <div id="states"></div>

                <label>City</label>
                <div id="towns"></div>
        </form>

UPDATE: usign JQuery 1.3

SOLUTION: brought by Daniel: replace the .change for .click

A: 

if you duplcate the id please make it unique

moustafa
Do yo mean the id's that I have deleted from the loaded pages? Those weren't on my original code.Or is there another error that I'm missing?
earlyriser
A: 

This is likely because you load the state after the original javascript runs (and assigns the .change() event.
Use the form of

$("select#woeid_state").live('change', function() {
    var admin1 = $("select#woeid_state option:selected").val(); 
    loadCities ( admin1 ); 
});

to capture the event on the select. EDIT: Put in the rest of the code

alternative as per note: add the event when you add the SELECT:

function AddSelectStateEvent()
{
$("select#woeid_state").change(function() { 
    var admin1 = $("select#woeid_state option:selected").val();  
    loadCities ( admin1 );  
}); 
};

and where you add the select:

AddSelectStateEvent();

NOTE:Fixed a syntax error in first option with .live (missing comma)

Mark Schultheiss
Mark:Where do I need to put this code?I have tried it -as a substitution for the $("#states").change-leaving the $("#states").change and adding it below it-inside the loadStates functionNone of these aproaches has resolved the problem. Do you have more clues?
earlyriser
replace the #states part - the #states div only really changes when it is loaded, not when the select within it changes (assuming it IS injected into the #states div)
Mark Schultheiss
NOTE: as an alternative, you could put it in a function, and call that function when you load the states select - I will add an example for that...
Mark Schultheiss
NOTE: fixed syntax error on first option, missing comma after the 'change'
Mark Schultheiss
Thanks Mark. I tried the first option. It works on FF, but I have the same problem in IE: it doesn't detect the change on the States dropdown
earlyriser
stupid me, forgot the :selected part duh, use the .val() for values and .text() for the text if you need that.
Mark Schultheiss
+1  A: 

It's counter-intuitive, but you need to use .click() instead of .change(). Internet Explorer doesn't fire the change event until the dropdown loses focus, while the other browsers fire change each time the value changes.

See this related Stack Overflow question: Getting jQuery to recognise .change() in IE.

Daniel Schilling
Thanks Daniel. I replaced the .change for .click and it works. However it still works when I replace only the ("#states").click(function() leaving untouched the ("#woeid_country").change(function(). How come IE requires a click only in the states part and not in the countries part?
earlyriser