Cascade plugin - having trouble with conditional cascades
I'm using the Cascade plugin (
http://plugins.jquery.com/project/cascade ) for a form and would like to conditionally populate a dropdown from one of several data sources depending on the selection made in a parent dropdown.
A simple example is to, given a list of states, populate a list of cities from an external data source for the selected state. Here is another user attempting to do precisely that:
When I attempt to use the code given in the response, however, the cascade does not appear to trigger. I'm no JavaScript nor jQuery expert, so I'm hoping somebody may be able to shed some light on how this should be done.
Below is an example of what I'm trying to do. Note that it does not work, even using a list instead of an external data source for the cascades.
Here's the JS:
- <script type="text/javascript">
- // Set up dropdown menu items for Cascade
- var az_list = [
- {'When':'AZ','Value':'Phoenix','Text':'Phoenix'},
- {'When':'AZ','Value':'Tucson','Text':'Tucson'},
- ];
- var ca_list = [
- {'When':'CA','Value':'San Francisco','Text':'San Francisco'},
- {'When':'CA','Value':'Los Angeles','Text':'Los Angeles'},
- ];
-
- // Set up some common stuff for Cascade
- function commonTemplate(item) {return '<option value="' + item.Value + '">' + item.Text + '</option>';};
- function commonMatch(selectedValue) {return this.When == selectedValue;};
-
- var defaultCascade = {
- template: commonTemplate,
- match: commonMatch,
- event: 'state.changed' //custom event type
- };
-
- // Set up Cascade definitions
- var az_cascade = $.extend({},defaultCascade,{ list: az_list });
- var ca_cascade = $.extend({},defaultCascade,{ list: ca_list });
-
- $(document).ready(function() {
- $('#state').change(function() {
- switch($('#state').val()) {
- case 'AZ':
- $('#city').cascade('#state',az_cascade);
- break
- case 'CA':
- $('#city').cascade('#state',ca_cascade);
- break
- default:
- alert('please select a state');
- }
- // Trigger that custom event
- $('#dimension_value').trigger('state.changed');
- });
- });
- </script>
And the HTML:
- <form>
- state
- <select id="state">
- <option value="">--Select--</option>
- <option value="AZ">Arizona</option>
- <option value="CA">California</option>
- </select><br/>
-
- city
- <select id="city"></select>
- </form>
Thanks in advance!
-Mike Babineau