1

I have two combo box , one for main category and other for sub category. Now , i want user to select main category whose values are coming from database , and based on that particular category, sub-category(from db) should be shown on the second combo box.

I have tried using java script as shown below:

     <script>
       function byId(e) {return document.getElementById(e);}

       function stateComboChange()
      {
  var combo1 = byId('stateCombo');  //main category combo id
  var combo2 = byId('cityCombo');    //sub category combo id
  //  alert(combo1.value);

  emptyCombo(combo2);
  switch(combo1.value)
  {
    case '-1':  addOption(combo2, -1, '-select state first-');
                break;
    case '0':   addOption(combo2,'Nokia');
                addOption(combo2, 'Samsung');
                addOption(combo2, 'Sony');
                addOption(combo2, 'Iphone 5');
                addOption(combo2, 'Lava');
                addOption(combo2, 'Micromax');

                break;
    case '1':   addOption(combo2, 'Edge Router');
                addOption(combo2, 'Subscriber Edge Router');
                addOption(combo2, 'Inter-provider Border Router');
                addOption(combo2, 'Core Router');
                addOption(combo2, 'Wired and Wireless Routers');

                break;
    case '2':       addOption(combo2, 'MTS');
                addOption(combo2, 'Docomo');
                addOption(combo2, 'Airtel');
                addOption(combo2, 'Photon');
                addOption(combo2, 'Idea');
                addOption(combo2, 'Vodafone');

                break;


}
cityComboChange();

//  var Cat = document.getElementById('stateCombo').value;
    document.getElementById('catvalue').value = combo1.value;

}

       function emptyCombo(e)
      {
         e.innerHTML = '';
      }

here i need to know how can i implement this add option function , so that values from db is shown on that 2nd combo box. right now this is manually.

       function addOption(combo, txt)
         {
      var option = document.createElement('option');
      option.value = txt;
        option.title = txt;
       option.appendChild(document.createTextNode(txt));
         combo.appendChild(option);
           }

        </script>

or can anyone suggest best possible solution for this.

4

0 回答 0