2010-07-18 97 views
4

我有這樣如何將當前選項標記爲選中狀態?

index.php?key=toplist&list=magic 

頁所以,如果該頁面例如在OM,我想在選擇菜單中選擇

<select name="skill" onchange="window.location.href=this.form.skill.options[this.form.skill.selectedIndex].value"> 
<option value="index.php?<?=QUERY_STRING?>&list=experience">Experience&nbsp;</option> 
<option value="index.php?<?=QUERY_STRING?>&list=magic">Magic</option> 
<option value="index.php?<?=QUERY_STRING?>&list=shielding">Shielding</option> 
<option value="index.php?<?=QUERY_STRING?>&list=distance">Distance</option> 
<option value="index.php?<?=QUERY_STRING?>&list=fishing">Fishing</option> 
</select> 

感謝

+0

off:這會是一個脛骨相關的網站? – Maerlyn 2010-07-18 16:25:06

回答

6

您將selected屬性添加到option標記。我通常這樣做是這樣的:

$lists = array('experience', 'magic', 'shielding', 'distance', 'fishing'); 
foreach($lists as $list) 
    echo "<option value=\"index.php?$QUERY_STRING&list=$list\"" . ($list == $_GET['list'] ? " selected" : "") . ">" . ucfirst($list) . "</option>" 
2

使用被標記魔術選項選定的屬性。在HTML中,這將是:

<option value="x" selected>Label</option> 

而在XHTML,這將是:

<option value="x" selected="selected">Label</option> 

這是一個HTML的問題,而不是一個PHP的問題,順便說一句。

3

對於每個<option>標籤,你要測試是否value對應一個是選擇要考慮,而且,對於一個就是,你必須添加selected屬性:

<option value="..." selected="selected">blah blah</option> 
相關問題