Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何基于另一个下拉列表中的选择在下拉列表中选择选项_Php_Javascript_Drop Down Menu - Fatal编程技术网

Php 如何基于另一个下拉列表中的选择在下拉列表中选择选项

Php 如何基于另一个下拉列表中的选择在下拉列表中选择选项,php,javascript,drop-down-menu,Php,Javascript,Drop Down Menu,我有两个下拉列表,它们已经从数据库中填充。如果在DropDownlist1中选择一个值,Dropdownlist2将获得与在DropDownlist1中选择的值相同的值 但是代码是基于开关箱的,而且选项也是硬编码的!在未来-许多选择可能会突然出现,它将不起作用 所以我想要的是,如果你在下拉列表1中选择一个选项,那么这个选项应该在下拉列表2中根据“值”而不是“索引”来选择 任何指点或帮助都将不胜感激!提前谢谢 function showSelected(f) { var selNum = f

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

但是代码是基于开关箱的,而且选项也是硬编码的!在未来-许多选择可能会突然出现,它将不起作用

所以我想要的是,如果你在下拉列表1中选择一个选项,那么这个选项应该在下拉列表2中根据“值”而不是“索引”来选择

任何指点或帮助都将不胜感激!提前谢谢

 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>     
功能显示已选择(f){
var selNum=f.type1.selectedIndex;
//var selText=f.type1.options[selNum].text
开关(selNum)
{
案例1:
document.getElementById('type2')。selectedIndex=2;
打破
案例2:
document.getElementById('type2')。selectedIndex=8;
打破
案例3:
document.getElementById('type2')。selectedIndex=3;
打破
案例4:
document.getElementById('type2')。selectedIndex=1;
打破
案例5:
document.getElementById('type2')。selectedIndex=4;
打破
案例6:
document.getElementById('type2')。selectedIndex=2;
打破
案例7:
document.getElementById('type2')。selectedIndex=2;
打破
案例8:
document.getElementById('type2')。selectedIndex=7;
打破
}
}
选择选项
函数选择选项()
{
var list=document.getElementById('list');
var listwo=document.getElementById('listwo');
var searchtext=list.options[list.selectedIndex].text;
对于(变量i=0;i

函数选择选项()
{
var list=document.getElementById('list');
var listwo=document.getElementById('listwo');
var searchtext=list.options[list.selectedIndex].text;
对于(变量i=0;i

打印选择框中的所有选项时,您可以提供与其在数组中的索引相关的选项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>

解析要发送给函数的“select”元素非常容易,因此您只需更改另一个select元素的selected选项,但是时间太晚了,我很累,所以我将留给您:)

当您打印出select框中的所有选项时,您可以在数组中提供与其索引相关的选项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>

解析要发送给函数的“select”元素非常容易,因此您只需更改另一个select元素的select选项,但是时间太晚了,我很累,所以我将把这留给您:)

在您的回答中,您需要修改
…list.options[I]。选择…
以列出两个选项。除此之外:很好!这个成功了!我使用这个值来代替文本,因为两个下拉列表都有不同的文本!非常感谢!太好了,tyvm:)在您的答案中,您需要修改
…list.options[i]。选择…
以列出两个。除此之外:很好!这个成功了!我使用这个值来代替文本,因为两个下拉列表都有不同的文本!非常感谢!太好了,泰文:)酷!谢谢-我接受了这个想法,并在ipr101给出的答案中给出了答案-帮助了我!酷!谢谢-我接受了这个想法,并在ipr101给出的答案中给出了答案-帮助了我!
function selected(selectElt) {
  var index = selectElt.selectedIndex;
  document.getElementById('first_combo_index_' + index).selected = true;
  document.getElementById('second_combo_index_' + index).selected = true;
}