如何在选项 HTML 标签中标记选定的选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10881448/
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 00:49:51  来源:igfitidea点击:

How to mark selected options in for the option HTML tag?

htmlcssdrop-down-menu

提问by peter

I am using normal select and mutliselect boxes on my site. should I use <option selected="selected">or simply <option selected>for selected items ?

我在我的网站上使用普通选择和多选择框。我应该使用<option selected="selected">还是仅<option selected>用于选定的项目?

回答by dvisor

Correct method according to W3C will be selected = "selected" as noted here http://www.w3.org/TR/html-markup/option.html#option

根据 W3C 的正确方法将被选择 = "selected" 如此处所述 http://www.w3.org/TR/html-markup/option.html#option

Quentin: Not duplicated because this question is related to select,option meanwhile your response is related to checkboxes

Quentin:不重复,因为这个问题与选择有关,而您的回答与复选框有关

(Same answer, different order to avoid "not an answer, it's a comment" comments for those unable to read a full post

(相同的答案,不同的顺序,以避免“不是答案,这是评论”对于无法阅读完整帖子的人的评论

回答by Oleg

If you want set selected more than 1 option in select, dont forget add to select attribute multiple and add to name of select [] else it doesnt work.

如果您想在选择中设置多于 1 个选项,请不要忘记添加选择属性多个并添加到选择 [] 的名称,否则它不起作用。

<select name="mega_select[]" multiple>
  <option value="some" selected="selected">some</option>
  <option value="value" selected="selected">value</option>
  <option value="non">non</option>
</select>