Html 如何在选择菜单中选择第一个空值选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2788398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:58:53  来源:igfitidea点击:

How to select first empty value option in a select menu?

htmlxhtmloption

提问by alex

The following always preselects the last option, even when I set the selectedattribute, but I like the first option to be preselected.

以下总是预选最后一个选项,即使我设置了selected属性,但我喜欢预选第一个选项。

<select name="filter">
    <option value="" selected>Make a choice</option>
    <option value="1">1</option>
    <option value="3">3</option>
    <option value="7">7</option>
    <option value="">all</option>
</select>

Can this be done?

这能做到吗?

回答by bradenkeith

<select name="filter">
    <option value="" selected="selected">Make a choice</option>
    <option value="1">1</option>
    <option value="3">3</option>
    <option value="7">7</option>
    <option value="">all</option>
</select>

If my code modification does not work. Check this article and make sure it's not something you're running in to http://www.beyondcoding.com/2008/12/16/option-selectedselected-not-working/

如果我的代码修改不起作用。检查这篇文章并确保它不是您遇到的http://www.beyondcoding.com/2008/12/16/option-selectedselected-not-working/

回答by ahebert

the correct implementation is:

正确的实现是:

<option value="1" selected>1</option>
<option value="9">9</option>