Problem:
In order to create a more interactive user experience, you want to populate a dropdown with options dynamically based on the selection made in a parent dropdown. You've attempted to do this using JavaScript, but you're facing compatibility issues in IE.
Solution:
To enhance compatibility and simplify the implementation, let's convert your JavaScript code to JQuery. Here's how you can achieve cascading dropdowns using JQuery:
<code class="javascript">jQuery(function($) { 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) }); });</code>
Explanation:
Demo:
Try out a live demo on Fiddle: https://jsfiddle.net/HvXSz/.
The above is the detailed content of How to Populate a Cascading Dropdown with JQuery in IE?. For more information, please follow other related articles on the PHP Chinese website!