验证 HTML 时出错:标签元素的 for 属性必须引用表单控件

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

Error validating HTML: The for attribute of the label element must refer to a form control

asp.net-mvc-3htmlvalidation

提问by Chan

I don't know why I keep getting this error while checking my page at http://validator.w3.org/checkThe error was:

我不知道为什么在http://validator.w3.org/check 上检查我的页面时总是收到此错误 错误是:

Line 46, Column 68: The for attribute of the label element must refer to a form control. 
<label class="environment-label" for="environment_form">Environments:</label>

I believe I provided an id reference for my labelto the outer form, why it keep bugging me about this error?

我相信我为我label的外部表单提供了一个 id 引用,为什么它一直困扰着我这个错误?

<div>
    <form id="environment_form" method="post">
        <div class="styled-select">
            <label class="environment-label" for="environment_form">Environments:</label>
            <select name="environment_dropdown" onchange="selectionChanged()">
                <option @(ViewData["selection"] == null || string.IsNullOrEmpty(ViewData["selection"].ToString()) ? "selected" : "")>select one</option>
                @foreach (string name in Model) { 
                    <option @(ViewData["selection"] != null && ViewData["selection"].Equals(name) ? "selected" : "")> 
                        @name
                    </option>
                }
            </select> 
        </div>
    </form>
</div>

回答by PoulsQ

you have this :

你有这个:

for="environment_form"

and it refers to the form directly ! But the "for" attribute should refer to an element of your form, in your case to the select. So add an "id" attribute to your select and change the "for", like this fo example :

它直接指的是表格!但是“for”属性应该引用表单的一个元素,在您的情况下是选择。因此,在您的选择中添加一个“id”属性并更改“for”,如下例:

<label class="environment-label" for="environment_dropdown">Environments:</label>
<select name="environment_dropdown" id="environment_dropdown" onchange="selectionChanged()">