Populate Dropdown Listbox based on another Listbox

Populate Dropdown Listbox based on another Listbox

I have the following select dropdowns:

  1. <select name="a" id="a">
  2. <option value="">Select Something</option>
  3. <option value="1">a1</option>
  4. <option value="2">a2</option>
  5. <option value="3">a3</option>
  6. </select>
  7. <select name="b" id="b"></select>
I use the following associative array (object?) to designate which values I would like displayed depending on whether a1, a2 or a3 is selected from the first dropdown:

  1. var somearray = [
  2.     {
  3.      "a1"  : ['a1.1']
  4.     },
  5.     {
  6.      "a2"  : ['a2.1','a2.2']
  7.     },
  8.     {
  9.      "a3"  : ['a3.1','a3.2','a3.3', 'a3.4']
  10.     }
  11.    ] ;
I'm not sure what I need to do next. I tried the following, and it loops through with the "name", which is 0, 1, 2, but not the value:

  1. $.each(somearray, function(name,value) {
  2.     $(":input[name='" + name + "']").val(value);
  3.     alert("test: " + name + value);
  4. });
Is the array correctly defined? Am I way off by using .each to populate the select listbox? Thanks for any pointers.






    • Topic Participants

    • dbv