How to select an option in a dropdown list based on a selection in another dropdown list(如何根据另一个下拉列表中的选择选择下拉列表中的选项)
问题描述
我有两个已经从数据库填充的下拉列表.如果您在 DropDownlist1 中选择一个值 - Dropdownlist2 获得与 Dropdownlist1 中选择的值相同的值.
I have two DropDown lists which are already populated from the database. If you select a value in DropDownlist1 - Dropdownlist2 gets the same value that was selected in Dropdownlist1.
但是代码是基于 switch case 的,而且选项也是硬编码的!将来 - 可能会出现许多选项,但它不会起作用!
But the code is based on switch case and also the options are hard coded ! In future - Many options might spring up and it will not work !
所以我想要的是如果您在下拉列表 1 中选择一个选项 - 应根据值"而不是索引"在下拉列表 2 中选择该选项 喜欢这里
So what I want is If you select an option in Dropdown list 1 - The option should be selected in DropDown list 2 based on the "value" and not "index" Like here
任何指示或帮助将不胜感激!提前致谢
Any pointers or help would be appreciated ! Thanks in advance
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>
推荐答案
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
这篇关于如何根据另一个下拉列表中的选择选择下拉列表中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何根据另一个下拉列表中的选择选择下拉列表中的选项
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
