2013-03-12 326 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