Home  >  Article  >  Web Front-end  >  How to Populate Cascading Dropdowns with jQuery for Improved Browser Compatibility?

How to Populate Cascading Dropdowns with jQuery for Improved Browser Compatibility?

Susan Sarandon
Susan SarandonOriginal
2024-10-28 02:33:02484browse

How to Populate Cascading Dropdowns with jQuery for Improved Browser Compatibility?

Populating Cascading Dropdowns with jQuery

Background

You've encountered a challenge when attempting to make a form with dynamic cascading dropdowns using both HTML and JavaScript. Your initial JavaScript code worked, but compatibility issues arose in Internet Explorer. Consequently, you're seeking to transition to jQuery for enhanced compatibility.

The Solution

The solution to your problem is surprisingly simple:

<br>jQuery(function($) {</p>
<pre class="brush:php;toolbar:false">var locations = {
    'Germany': ['Duesseldorf', 'Leinfelden-Echterdingen', 'Eschborn'],
    'Spain': ['Barcelona'],
    'Hungary': ['Pecs'],
    'USA': ['Downers Grove'],
    'Mexico': ['Puebla'],
    'South Africa': ['Midrand'],
    'China': ['Beijing'],
    'Russia': ['St. Petersburg'],
}

var $locations = $('#location');
$('#country').change(function () {
    var country = $(this).val(), lcns = locations[country] || [];

    var html = $.map(lcns, function(lcn){
        return '<option value="' + lcn + '">' + lcn + '</option>'
    }).join('');
    $locations.html(html)
});

});

Demo

Check out a live demonstration of this solution on [Fiddle](fiddle link here).

The above is the detailed content of How to Populate Cascading Dropdowns with jQuery for Improved Browser Compatibility?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn