問題描述
我有兩個已經從數據庫填充的下拉列表.如果您在 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
這篇關于如何根據另一個下拉列表中的選擇選擇下拉列表中的選項的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!