Solved

I need a set of Conditional Select Menus Using JQuery (Country, State, County, City)

Posted on 2010-08-20
1
1,078 Views
Last Modified: 2012-05-10
I need a set of HTML Select menus that are conditional on the one above it.  The top level is the Country.  Depending upon the Country, a list of States of Providences need to populate the next menu.  Then a list of Counties in the State or province in the next menu.  Then a list of Cities based on the county.  I already have a US Zipcode database that has all the data.  I just don't know anything about JQuery and need help putting together the elements.

I am in a jam and offer 500 points to whoever can provide me with the needed code.

Phil
0
Comment
Question by:SiriusPhil
1 Comment
 
LVL 9

Accepted Solution

by:
matthew016 earned 500 total points
ID: 33489746
You need to bind the event "change" on your select elements.

e.g.

$('select#country').change(function() {
...
}

Each time a select element changes (country, states, etc.), you need to retrieve by AJAX the appropriate sublists and replace those by the old ones.

e.g.
a country has been selected, refresh the <select> states :

$.ajax({
    type: "GET",
    dataType: "json",
    async: false,
    url: "/ajax-get-states?country=" + countrySelected,
    success: function(data){
        $.each(data, function() {
            /* populate the <select> element of states */
            $('<option>').val(this[0]).text(this[1]).appendTo('select#states');
        });
    },
    error: function(xhr, status, ex) {}
});
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now