html选择标签问题

发布于 2024-08-04 15:26:32 字数 478 浏览 3 评论 0原文

在此处输入代码我正在使用以下代码。我想选择唯一的一个选项。但现在它显示了页面加载时默认选择的一些其他选项。 我怎样才能具体选择我的选择?

<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 herei 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一梦浮鱼 2024-08-11 15:26:32

仅所选属性的存在就足以使选项被选中。您需要从第二个选项中删除 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.

纸短情长 2024-08-11 15:26:32

浏览器通常只检查所选属性是否存在。因此,您应该将代码更改为:

<select name="ms">                                                                                    
        <option value="-1">any</option>
        <option value="0" selected="selected">only single</option>
        <option value="1">only married</option>
</select>

编辑:看起来您编辑了示例,所以我将编辑我的示例。

Browsers generally only check if the selected attribute exists. Therefore you should change your code to:

<select name="ms">                                                                                    
        <option value="-1">any</option>
        <option value="0" selected="selected">only single</option>
        <option value="1">only married</option>
</select>

EDIT: Looks like you edited your example so I'll edit mine.

回眸一遍 2024-08-11 15:26:32

HTML4 + HTML5:使用属性最小化;因此,对所选选项使用属性“selected”(其他选项没有属性)

<select name="ms">                                                                                       <option value="-1" selected="false"  >any</option>
            <option value="0" selected>only single</option>
            <option value="1">only married</option>
</select>

XHTML:禁止属性最小化,这意味着您需要为属性分配一个值,即 selected="selected" (这是它所采用的唯一值)

<select name="ms">                                                                                       <option value="-1" selected="false"  >any</option>
            <option value="0" selected="selected">only single</option>
            <option value="1">only married</option>
</select>

检查 html 页面/文件的 DOCTYPE,看看您使用的是 XHTML 还是 HTML。

HTML4 + HTML5 : use attribute minimization; therefore use the attribute 'selected' for the selected option (no attribute for the others)

<select name="ms">                                                                                       <option value="-1" selected="false"  >any</option>
            <option value="0" selected>only single</option>
            <option value="1">only married</option>
</select>

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)

<select name="ms">                                                                                       <option value="-1" selected="false"  >any</option>
            <option value="0" selected="selected">only single</option>
            <option value="1">only married</option>
</select>

Check the DOCTYPE of your html page/file to see if you use XHTML or HTML.

吃颗糖壮壮胆 2024-08-11 15:26:32

如果未选择该选项,则根本不提供“selected”属性。这样效果会更好:

<select name="ms">
   <option value="-1" >any</option>
   <option value="0" selected >only single</option>
   <option value="1" >only married</option>
</select>

Don't supply the "selected" attribute at all, if the option isn't selected. This will work better:

<select name="ms">
   <option value="-1" >any</option>
   <option value="0" selected >only single</option>
   <option value="1" >only married</option>
</select>
殤城〤 2024-08-11 15:26:32
<option value="0" selected="selected" >only single</option>
            <option value="1" >only married</option>
<option value="0" selected="selected" >only single</option>
            <option value="1" >only married</option>
我不吻晚风 2024-08-11 15:26:32
<select name="ms">
    <option value="-1" selected="false"  >any</option>
    <option value="0" selected="true" >only single</option>
    <option value="1">only married</option>
</select>
<select name="ms">
    <option value="-1" selected="false"  >any</option>
    <option value="0" selected="true" >only single</option>
    <option value="1">only married</option>
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文