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

相關問題