我从数据库中提取记录并将其插入下拉列表中:
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>";
答案 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;
}
您可以更新您的PHP代码,以在每个option
中包含一个值:
echo "<option value=\"urlhere.php\">" . $drow['id'] . "</option>";