开发者

How do I mark current option as selected?

开发者 https://www.devze.com 2023-01-07 14:57 出处:网络
I have pages like index.php?key=toplist&list=magic So if om on that page for example, i want the Magic option to be marked as selected in the select menu

I have pages like

index.php?key=toplist&list=magic

So if om on that page for example, i want the Magic option to be marked as selected in the select menu

<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>
开发者_如何学运维

Thanks


You add the selected attribute to the option tag. I typically do it with something like this:

$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>"


For each <option> tag, you have to test if the value corresponds to the one that is to be considered as selected, and, for the one that is, you have to add the selected attribute :

<option value="..." selected="selected">blah blah</option>


Use the selected attribute. In HTML, this would be:

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

And in XHTML, it would be:

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

This is an HTML question, not a PHP-question, by the way.

0

精彩评论

暂无评论...
验证码 换一张
取 消