首页 > 解决方案 > 如何使用 php 通过 id 获取选择选项

问题描述

我想使用 php 通过 id 访问下拉菜单选项,就像我在 javascript 中所做的那样。我也想在 php 中完成这项工作。有没有办法做到这一点。请指导。我的 JS 代码是。

var opt = amove.options[amove.selectedIndex];

   if(opt.id =="Exchange"){          
      document.getElementById('barcode2lb').style.display='block';

      document.getElementById('barcode2txt').style.display='block';
}
else{

     document.getElementById('barcode2lb').style.display='none';

     document.getElementById('barcode2txt').style.display='none';
}  

标签: php

解决方案


例如,

<select>
<?php foreach($yourArr as $row):
if(yourConidtion){
  $selected='selected';
}else{
  $selected='';
}?>
<option value="<?= $row->id?>" <?= $selected ?>><?= $row->name ?></option>
<?php endforeach;?>
</select>

我想这就是你要求的


推荐阅读