下拉选项作为锚点

时间:2012-03-15 16:42:34

标签: php

我从数据库中提取记录并将其插入下拉列表中:

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

它有效但我需要能够点击下拉列表中的一个选项并将其链接为:

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

更新:最新代码:

<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 个答案:

答案 0 :(得分:4)

您不能在选择列表中的选项上放置锚点。

您可以使用JavaScript,然后对选择列表的change事件执行某些操作:

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

然后在JavaScript中根据所选值执行某些操作:

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

Example here

您可以更新您的PHP代码,以在每个option中包含一个值:

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