【问题标题】:How to select an option in a dropdown list based on a selection in another dropdown list如何根据另一个下拉列表中的选择来选择下拉列表中的选项
【发布时间】:2011-08-06 08:11:20
【问题描述】:

我有两个已经从数据库中填充的下拉列表。如果您在 DropDownlist1 中选择一个值 - Dropdownlist2 将获得与在 Dropdownlist1 中选择的值相同的值。

但是代码是基于 switch case 的,而且选项是硬编码的!将来 - 许多选项可能会出现,但它不会起作用!

所以我想要的是如果您在下拉列表 1 中选择一个选项 - 应根据“值”而不是“索引”在下拉列表 2 中选择该选项Like here

任何指针或帮助将不胜感激!在此先感谢

 function showSelected(f) {

var selNum  = f.type1.selectedIndex;
//var selText = f.type1.options[selNum].text


switch (selNum)
{
case 1:
document.getElementById('type2').selectedIndex= 2;
break;

case 2:
document.getElementById('type2').selectedIndex = 8;
break;

case 3:
document.getElementById('type2').selectedIndex = 3;
break;

case 4:
document.getElementById('type2').selectedIndex = 1;
break;

case 5:
document.getElementById('type2').selectedIndex = 4;
break;

case 6:
document.getElementById('type2').selectedIndex = 2;
break;

case 7:
document.getElementById('type2').selectedIndex = 2;
break;

case 8:
document.getElementById('type2').selectedIndex = 7;
break;

}


}

 <select name="Type1" id="Type1" onchange="showSelected(this.form)" >
<option>Select Option</option>
    <option  value="<?php echo $record->getID();?>" > <?php echo $record->getIDName();?> </option>
 </select>       

 <select name="Type2" id="Type2" disabled>
<option>Select Option</option>
    <option  value="<?php echo $record->getID();?>" ><?php echo $record->getIDValue();?> </option>
 </select>     

【问题讨论】:

    标签: php javascript drop-down-menu


    【解决方案1】:
    function selectoption()
        {
            var list = document.getElementById('list');
            var listtwo = document.getElementById('listtwo');
            var searchtext = list.options[list.selectedIndex].text; 
            for(var i = 0; i < listtwo.options.length; ++i)
                if (listtwo.options[i].text === searchtext) listtwo.options[i].selected = true;
        }
    

    http://jsbin.com/oyaloc/2

    【讨论】:

    • 在您的回答中,您需要将...list.options[i].select... 修改为列出两个。除此之外:很好!
    • 这个成功了!我使用了该值而不是文本,因为两个下拉列表都有不同的文本!非常感谢!
    • 完美,tyvm :)
    【解决方案2】:

    当您打印出选择框中的所有选项时,您可以提供与其在数组中的索引相关的选项 ID:

    <select id='first_combo_box' onchange='selected(this);'>
    <?php
      foreach( $arrayOfOptions as $index => $option ):
    ?>
        <option value='<?php echo $option?>' id='first_combo_index_<?php echo $index?>'><?php echo $option ?></option>
    <?php
      endforeach;
    ?>
    </select>
    
    <select id='second_combo_box' onchange='selected(this);'>
    <?php
      foreach( $arrayOfOptions as $index => $option ):
    ?>
        <option value='<?php echo $option?>' id='second_combo_index_<?php echo $index?>'><?php echo $option ?></option>
    <?php
      endforeach;
    ?>
    </select>
    

    然后在你的javascript中:

    function selected(selectElt) {
      var index = selectElt.selectedIndex;
      document.getElementById('first_combo_index_' + index).selected = true;
      document.getElementById('second_combo_index_' + index).selected = true;
    }
    

    解析您要发送给函数的“选择”元素非常容易,因此您只需更改 OTHER 选择元素的选定选项,但为时已晚,我累了,所以我会把它留给你:)

    【讨论】:

    • 酷!谢谢-我接受了这个想法并提出了ipr101给出的答案-帮助了我!
    猜你喜欢
    • 2017-01-11
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多