2013-01-20 106 views
2

我有這樣的設置:jQuery來只顯示選中的複選框的標籤和隱藏未檢查

<style>.selectit{display:none;}</style> 
<div id="foo"> 
<label class="selectit"> 
<input type="checkbox" name="one" id="one" checked> 
One 
</label> 
<label class="selectit"> 
<input type="checkbox" name="two" id="two"> 
Two 
</label> 
<label class="selectit"> 
<input type="checkbox" name="three" id="three" checked> 
Three 
</label> 
</div> 

我只想顯示選中輸入的標籤標籤和隱藏標籤等。

類似show()爲checked選項標籤和隱藏()爲未選中的。工作代碼

例如:

<style>.selectit{display:none;}</style> 
<div id="foo"> 
<label class="selectit" style="display:block"> 
<input type="checkbox" name="one" id="one" checked> 
One 
</label> 
<label class="selectit"> 
<input type="checkbox" name="two" id="two"> 
Two 
</label> 
<label class="selectit" style="display:block"> 
<input type="checkbox" name="three" id="three" checked> 
Three 
</label> 
</div> 

也許也許,做一個新的div並只顯示該分區的檢查標籤的文本。

<style>.selectit{display:none;}</style> 
<div id="foo"> 
<label class="selectit"> 
<input type="checkbox" name="one" id="one" checked> 
One 
</label> 
<label class="selectit"> 
<input type="checkbox" name="two" id="two"> 
Two 
</label> 
<label class="selectit"> 
<input type="checkbox" name="three" id="three" checked> 
Three 
</label> 
</div> 
<div id="output">One - Three</div> 

這甚至有可能嗎?或者我需要通過追加一個搜索整個複選框的div來做到這一點?任何幫助都是值得讚賞的,因爲我已經用盡了所有選項,而且我沒有想法。先謝謝你。

+1

我建立與每個選中的複選框引用的對象,以及與每個標籤引用的對象。然後,我會查看標籤列表,並查看「for」屬性是否與任何選中的複選框匹配。另外,添加'for'屬性。這對於可訪問性很好。 – joequincy

回答

3

您可以使用filter方法過濾選中的複選框。

$('#foo input[type=checkbox]').filter(function(){ 
     return this.checked; 
}).prev('label').show(); 
2

免責聲明:這個答案正是基於你的標題問題


我會去用純CSS 那個。像

label { 
    display: none; 
} 

input:checked + label { 
    display: block; 
} 

你只需要調整你的標記一點。

實施例:http://jsfiddle.net/ZpmGd/

+0

這只是整潔,正如我可以看到標籤嵌套正確時的作品。但是我正在編輯一個插件,而我實際上並不具備編輯插件製造商原始標記的知識。我希望我可以使用純CSS來做到這一點,因爲這很好,我只是後悔使用jQuery。儘管jQuery方法現在對我來說似乎是最簡單的方法。榮譽的答案。 –

相關問題