如何将当前选项标记为已选中?

Cha*_*lie 4 html php

我有像这样的页面

index.php?key=toplist&list=magic
Run Code Online (Sandbox Code Playgroud)

因此,如果在该页面上使用om,我希望Magic选项在选择菜单中标记为已选中

<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>
Run Code Online (Sandbox Code Playgroud)

谢谢

Mic*_*zek 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>"
Run Code Online (Sandbox Code Playgroud)