0

我正在尝试将文本值存储到来自两个选择标签的输入标签中,其中第二个选择标签的值取决于第一个。有一个 javascript 正在为此运行,它看起来像这样:

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

  function categoryComboChange()
  {
  var combo1 = byId('categoryCombo');
 var combo2 = byId('subcategoryCombo');
 alert(combo1.value);

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

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

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

                break;
    }
  subcategoryComboChange();
      }

   function subcategoryComboChange()
      {

  var Category = document.getElementById('subcategoryCombo').value;







// Used desired display of following commands 

document.getElementById('cvalue').value = Category;





     }

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

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

  </script>

我的带有必需元素的 jsp 页面如下所示:

        <div class="element">
                    <label for="category">Category </label>
                    <select id='categoryCombo'             onchange='categoryComboChange();'>
    <option value='-1' title='-select one-'>-select one-</option>
    <option value='0' title='Mobile'>Mobile</option>
    <option value='1' title='Nsw'>Routers</option>
    <option value='2' title='Tas'>Datacard</option>
</select>
                                        </div>

                <div class="element">
                    <label for="category">Sub-Category </label>
                     <select id='subcategoryCombo' name="category" onchange='subcategoryComboChange();'>
    <option value='-1' title='-select category first-'>-select category first-</option>
</select>
                </div>


                  <div class="element">
                <input id="cvalue" name="cval" class="text err" />

                        </div>                      

目前,我能够以数字形式获取值,例如 0、1、2,具体取决于选择,但我需要在输入标签中显示文本值,例如移动设备,它应该显示诺基亚、三星、等任何人都可以建议我需要在哪里更改我的代码以执行此任务吗?

4

1 回答 1

2

尝试:

function subcategoryComboChange()
{
 var Category = document.getElementById("subcategoryCombo");
var subCat = Category.options[Category.selectedIndex].text;
document.getElementById('cvalue').value = subCat;
}

DEMO FIDDLE

于 2013-09-07T06:58:54.507 回答