html选择标签问题
在此处输入代码
我正在使用以下代码。我想选择唯一的一个选项。但现在它显示了页面加载时默认选择的一些其他选项。 我怎样才能具体选择我的选择?
<select name="ms"> <option value="-1" selected="false" >any</option>
<option value="0" selected="true" >only single</option>
<option value="1" selected="false" >only married</option>
</select>
enter code here
i am using the folllowing code . I wnat that the only single option is selected. but right now its showing some other options as selected by defaul when the page loads.
How can i make one particular of my choice o selected?
<select name="ms"> <option value="-1" selected="false" >any</option>
<option value="0" selected="true" >only single</option>
<option value="1" selected="false" >only married</option>
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
仅所选属性的存在就足以使选项被选中。您需要从第二个选项中删除
selected="false"
文本才能完成此操作。 selected 和disabled 在这方面是相似的。The selected attribute's presence alone is enough to make the option selected. You will need to remove the
selected="false"
text from the second option to make this work. selected and disabled are similar in this respect.浏览器通常只检查所选属性是否存在。因此,您应该将代码更改为:
编辑:看起来您编辑了示例,所以我将编辑我的示例。
Browsers generally only check if the selected attribute exists. Therefore you should change your code to:
EDIT: Looks like you edited your example so I'll edit mine.
HTML4 + HTML5:使用属性最小化;因此,对所选选项使用属性“selected”(其他选项没有属性)
XHTML:禁止属性最小化,这意味着您需要为属性分配一个值,即 selected="selected" (这是它所采用的唯一值)
检查 html 页面/文件的 DOCTYPE,看看您使用的是 XHTML 还是 HTML。
HTML4 + HTML5 : use attribute minimization; therefore use the attribute 'selected' for the selected option (no attribute for the others)
XHTML : attribute minimization is forbidden, meaning you need to assign a value to the attribute, i.e. selected="selected" (which is the only value it takes)
Check the DOCTYPE of your html page/file to see if you use XHTML or HTML.
如果未选择该选项,则根本不提供“selected”属性。这样效果会更好:
Don't supply the "selected" attribute at all, if the option isn't selected. This will work better: