简体   繁体   English

是否可以在表单的标签内使用标签?

[英]Is it possible to use labels within labels in a form?

I am building a form which uses labels to define the input values (as many do). 我正在构建一个使用标签定义输入值的表单(许多这样做)。

A sample of this form can be seen here. 这种形式的样本可以在这里看到。

My issue, however, is in using things like checkboxes and radio buttons. 但是,我的问题是使用复选框和单选按钮之类的东西。 While I want to define a group of input options, (say something like Gender), using to then surround my actual inputs with "male" and "female", it seems to cause conflicts, specifically styling wise given it takes the original css attributes. 虽然我想定义一组输入选项(例如Gender之类的东西),然后使用它们将我的实际输入内容用“ male”和“ female”包围,但这似乎会引起冲突,特别是考虑到它采用了原始css属性,在样式上是明智的。

What is the best/proper way to use labels within labels where the first label will define an overall area and the subsequent labels are used to merely define the specific input? 在标签内使用标签的最佳/正确方法是什么,其中第一个标签将定义整个区域,而后续标签仅用于定义特定输入?

EDIT 编辑

The form I demo above is an example of an issue I am facing but for consistency, I plan to use this form styling across several (50+) pages of form collection so while possible, I am trying not to target a specific issue, rather solve something across many pages on my site. 我上面演示的表单是我面临的一个问题的示例,但是为了保持一致性,我计划在几(50+)页的表单集合中使用此表单样式,因此,在可能的情况下,我尝试不针对特定的问题,而是解决我网站上许多页面上的问题。

I think you should use fieldset and legend tags instead, eg 我认为您应该改用fieldsetlegend标签,例如

<fieldset>
   <legend>Gender</legend>

   <label for="male"><input type="radio" ... />male</label>
   <label for="female"><input type="radio" ... />female</label>
</fieldset>

fieldset is a logical container for grouping related inputs and legend represents an introductory caption for the fieldset itself. fieldset是用于对相关输入进行分组的逻辑容器, legend表示该字段集本身的介绍性标题。 Your fiddle updated: http://jsfiddle.net/zrqnT/4/ 您的提琴已更新: http : //jsfiddle.net/zrqnT/4/

see also: 也可以看看:
http://dev.w3.org/html5/spec/single-page.html#the-fieldset-element http://dev.w3.org/html5/spec/single-page.html#the-fieldset-element
http://dev.w3.org/html5/spec/single-page.html#the-legend-element http://dev.w3.org/html5/spec/single-page.html#the-legend-element

Here is the specification: 这是规格:

http://www.w3.org/TR/html5/forms.html#the-label-element http://www.w3.org/TR/html5/forms.html#the-label-element

There it says: 那里说:

The for attribute may be specified to indicate a form control with which the caption is to be associated. 可以指定for属性来指示与标题相关联的表单控件。 If the attribute is specified, the attribute's value must be the ID of a labelable form-associated element in the same Document as the label element. 如果指定了属性,则该属性的值必须是与label元素在同一Document中与可标签的表单相关元素的ID。 If the attribute is specified and there is an element in the Document whose ID is equal to the > value of the for attribute, and the first such element is a labelable form-associated > element, then that element is the label element's labeled control. 如果指定了属性,并且文档中有一个元素的ID等于for属性的>值,并且第一个这样的元素是可标签的与表单相关的>元素,则该元素是标签元素的标签控件。

If the for attribute is not specified, but the label element has a labelable form-associated element descendant, then the first such descendant in tree order is the label element's labeled control. 如果未指定for属性,但label元素具有可贴标签的与表单相关的元素后代,则树顺序中的第一个此类后代是label元素的带标签的控件。

So, you must provide a for attribute. 因此,您必须提供一个for属性。 But this can just be the case if you have a form control with the same id. 但是,如果您有一个具有相同ID的表单控件,可能就是这种情况。 It's not the case here. 这里不是这样。
Otherwise you can surround your code with a label, but then your elements must be labelable. 否则,您可以在代码周围加上标签,但是元素必须是可标签的。

Labelable Elements are: 可标记元素是:

Labelable elements 可标记元素
Denotes elements that can be associated with label elements. 表示可以与标签元素关联的元素。

button input (if the type attribute is not in the hidden state) keygen meter output progress select textarea 按钮输入(如果type属性未处于隐藏状态)keygen meter输出进度选择textarea

So no chance for a label to have this specific behavior in your case 因此,根据您的情况,标签没有机会具有这种特定行为

Use fieldset and legend instead 使用字段集和图例代替

<fieldset>
    <legend>Gender: <em>*</em></legend>
    <label for="male"><input type="radio" name="gender" id="male" value="m" class="required" role="input" aria-required="true" />male</label>
    <label for="female"><input type="radio" name="gender" id="female" value="f" class="required" role="input" aria-required="true" />female</label>
</fieldset>

Simply use this CSS 只需使用此CSS

#formWrapper label[for=male], #formWrapper label[for=female] { color: red; } 

This will allow you to change just those labels via straight CSS. 这将允许您通过直接CSS更改那些标签。 The other option is to just use containing divs. 另一种选择是仅使用包含div。

<div class="form-item">
    <label>Gender</label>
    <div class="radios">
         <label for="male">Male</label><input type="radio" for="male"/>
         <label for="female">Female</label><input type="radio" for="female"/>
    </div>
</div>

What Fabrizio said might work, but personally I would just use a heading. Fabrizio所说的可能有用,但我个人仅使用标题。 You don't necessarily have to use form elements for forms. 您不必一定要为表单使用表单元素。 Plus using a normal heading element would give you much greater flexibility as far as styling goes. 另外,使用常规的标题元素会给您更大的灵活性。

<div class="stage clear">
    <h3>Gender: <em>*</em></h3>
    <label for="male"><input type="radio" name="gender" id="male" value="m" class="required" role="input" aria-required="true" />male</label>
     <label for="female"><input type="radio" name="gender" id="female" value="f" class="required" role="input" aria-required="true" />female</label>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM