2012-07-19 25 views
8

我不知道为什么,我不断收到此错误,同时检查我的网页在http://validator.w3.org/check 错误是:错误验证HTML:该为标签元素必须引用表单控件的属性

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> 

我我相信我为我的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> 

回答

26

你有这样的:

for="environment_form" 

,并直接引用的形式!但是“for”属性应该引用表单中的一个元素,就你的情况而言,指向select。因此,添加一个“id”属性到您的选择并更改“for”,如下所示:

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

谢谢。我知道了 ;) – Chan 2012-07-19 19:00:58

相关问题