2012-03-15 37 views
0

我從數據庫中提取出的記錄和插入他們一個下拉列表裏是這樣的:選項上下拉錨

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> 

UPDATE:最新代碼:

<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

這是甚至可能與HTML? – h00ligan 2012-03-15 16:45:22

+0

@ h00ligan:不是。你需要JavaScript。 – 2012-03-15 16:53:21

+1

另外,對於OP。我不建議你這樣做。有一個提交按鈕並正確地重定位用戶。從選擇輸入來重定向頁面是意想不到的。 – 2012-03-15 16:54:34

回答

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>"; 
+0

經過測試,但onchange我的網址獲得「未定義」添加到它 – Satch3000 2012-03-15 16:56:56

+0

@ Satch3000更新了'doSomething'函數的第一行 - 試試 – ManseUK 2012-03-15 17:00:27

+0

測試了這個變化,但現在什麼都沒做 – Satch3000 2012-03-15 17:17:28