doushenyi9104 2012-03-15 16:42
浏览 51
已采纳

下拉选项作为锚点

I am pulling out record from the database and inserting them inside a dropdown like this:

echo "<select>";
while ($drow = mysql_fetch_assoc($request))
{
   echo "<option>" . $drow['id'] . "</option>";
}
echo "</select>";

It works but I need to be able to click on an option on the dropdown and make it link just like:

<a href="Record1Here">Record1Here</a>
<a href="Record2Here">Record2Here</a>
<a href="Record3Here">Record3Here</a>

UPDATE: Latest code:

<script>

function doSomething() {
    var currentval = this.options[this.selectedIndex].value;
    // you could navigate away at that point ?
    window.location = currentval;
}
</script>

...

echo "<select onchange='doSomething();'>";
while ($drow = mysql_fetch_assoc($request))
{

   echo "<option value=\"view.php\">" . $drow['id'] . "</option>";

}
echo "</select>";
  • 写回答

1条回答 默认 最新

  • dtwye28880 2012-03-15 16:46
    关注

    You can't place anchors on an option within a select list.

    What you can do is use JavaScript and then do something on the change event of the select list :

    echo "<select onchange='doSomething(this);>';
    

    then in JavaScript do something based on the selected value :

    function doSomething(elem) {
        var currentval = elem.options[elem.selectedIndex].value;
        // you could navigate away at that point ?
        window.location = currentval;
    }
    

    Example here

    you could update your PHP code to include a value in each option :

    echo "<option value=\"urlhere.php\">" . $drow['id'] . "</option>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题