Last active
December 4, 2015 19:59
-
-
Save v123582/3d2e2f0ac31e994c7dc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // demo: http://jsfiddle.net/v123582/j6r454om/ | |
| <select class="mobile-vendor-1"> | |
| <option value="motorola">Motorola</option> | |
| <option value="nokia">Nokia</option> | |
| <option value="android">Android</option> | |
| </select> | |
| <select class="model-1"> | |
| <option></option> | |
| </select> | |
| <select class="mobile-vendor-2"> | |
| <option value="motorola">Motorola</option> | |
| <option value="nokia">Nokia</option> | |
| <option value="android">Android</option> | |
| </select> | |
| <select class="model-2"> | |
| <option></option> | |
| </select> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function dynamicSelect($vendor, $model, selectValues) { | |
| $vendor.change(function () { | |
| if (selectValues[$vendor.val()]) { | |
| $model.empty().append(function () { | |
| var output = ''; | |
| $.each(selectValues[$vendor.val()], function (key, value) { | |
| output += '<option value' + value + '>' + key + '</option>'; | |
| }); | |
| return output; | |
| }); | |
| } | |
| }).change(); | |
| } | |
| $(function () { | |
| var selectValues1 = { | |
| "nokia": { | |
| "N971": "http://www.google.com", | |
| "N931": "http://www.stackoverflow.com" | |
| }, | |
| "motorola": { | |
| "M11": "http://www.ebay.com", | |
| "M21": "http://www.twitter.com" | |
| } | |
| }; | |
| var selectValues2 = { | |
| "nokia": { | |
| "N972": "http://www.google.com", | |
| "N932": "http://www.stackoverflow.com" | |
| }, | |
| "motorola": { | |
| "M12": "http://www.ebay.com", | |
| "M22": "http://www.twitter.com" | |
| } | |
| }; | |
| var $vendor1 = $('select.mobile-vendor-1'); | |
| var $model1 = $('select.model-1'); | |
| var $vendor2 = $('select.mobile-vendor-2'); | |
| var $model2 = $('select.model-2'); | |
| dynamicSelect($vendor1, $model1, selectValues1); | |
| dynamicSelect($vendor2, $model2, selectValues2); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment