HTML验证报错:label元素for属性需关联非隐藏表单控件求助
Hey there! Let's sort out that validation error you're running into. The root of the problem is how your <label> is connected to the input element:
The for attribute on a <label> needs to match the id attribute of the form control it's labeling, but right now your <input> only has a name="Country" attribute—no matching id="Country". That's why the validator is throwing that error.
Here's the corrected version of your code:
<div class="needContent"> <label for="Country">Country</label> <input list="browsers" name="Country" id="Country" required="required"> <datalist id="browsers"> <option value="Canada"></option> <option value="The United States"></option> <option value="India"></option> <option value="Pakistan"></option> <option value="Germany"></option> </datalist> </div>
Let me break down the key changes:
- Added
id="Country"to the<input>element: Now the<label>'sfor="Country"correctly targets this visible input control, which resolves the validation error. - Closed all
<option>tags: While HTML allows unclosed option tags, closing them makes the code more consistent and easier to maintain.
Once you make these changes, the validator should pass without that error. 😊
内容的提问来源于stack exchange,提问作者J.M
相关产品推荐
相关产品推荐

