2013-03-12 148 views
0

我想在不影响嵌套<input>标签的情况下禁用或隐藏内容“分组” <label>标签。隐藏标签

<label class="" for="officersheet_fields_attributes_3_grouping"> 
<input type="checkbox" id="officersheet_fields_attributes_3_grouping" name="officersheet[fields_attributes][3][grouping]" value="1"> 
Grouping 
</label>` 

我在rails中使用formtastic。 formtastic代码片段 <td><%= f.input :grouping %></td>

上面的代码生成上面的html代码。

在此先感谢

回答

0

您可以使用text-indent: -1000em

label 
{ 
    text-indent: -1000em; 
} 

但我不认为这是一个好主意,有标签内的输入。最好有以下几点:

<input type="checkbox"/><label>Grouping</label> 
+0

把'input'放在'label'里面怎么回事? W3C表示,可以在'input'> http://www.w3.org/TR/html401/interact/forms.html#edef-LABEL – BenM 2013-03-12 09:20:06

+0

之前,之后或周围放置'label',问题在于它是由rails中的formtastic自动生成的。代码'​​<%= f.input:grouping%>' – Tony45 2013-03-12 09:22:15

+0

当然这是可能的,也是有效的。但标签是输入控件的可点击文本。把输入控制放在里面不是很干净,我想。此外,当你将它们分开时,你会更加灵活。 – 2013-03-12 09:22:22

0

添加span标签周围的标签文字和隐藏

<label for="foo"> 
    <input type="checkbox" value="1"><span>Grouping</span> 
</label> 

CSS

span{ 
    display:none 
} 

DEMO

0

我会去的跨度太大,但是如果你没有控制l在你的html结构上,你可以这样做:

$(document).ready(function() { 
    $('label') 
     .contents() 
     .each(function() { 
      // if (this.nodeType == Node.TEXT_NODE); this works unless using IE 7 
      if (this.nodeType === 3) { 
       $(this).remove(); 
      } 
     }); 
}); 
+0

感谢Robbert van den Bogerd – Tony45 2013-03-13 05:01:45

+0

@ Tony45:请您将此答案标记为已接受? – 2013-10-16 09:21:12