2015-11-04 24 views
0

在jQuery中,我想獲取具有特定類的div中的廣播和複選框的所有輸入控件。除非該div內有另一個set類的div。選擇器會在div內輸入什麼

例如

<div class="content-wrap"> 
<div><input id="chk1" type="checkbox"/></div> 
<div class="normalCheck"><input id="chk2" type="checkbox" /></div> 
</div> 

在這個例子中,我想在 'CHK1' 但不是在

我已經試過這

$('.content-wrap,.profile-acct-btn').not('.normalCheck').('input[type="checkbox"],input[type="radio"]').each(function() {} 

但這並不 'CHK2' 執行某些功能似乎得到它 - 我哪裏錯了?

回答

0

在CSS:類型無線電複選框的輸入字段選擇器看起來像這樣

.content-wrap > div > input[type="radio"], .content-wrap > div:not(.normalCheck) > input[type="checkbox"] 

在jQuery中,你應該使用上述選擇像這樣:

var radioCheckboxInput = $('.content-wrap > div > input[type="radio"], .content-wrap > div:not(.normalCheck) > input[type="checkbox"]'); 

實現這個選擇器就像使用變量radioCheckboxInput一樣簡單,如下所示:

radioCheckboxInput.each(function(){ 
    //... Do something 
}); 
2

,而不是jQuery的not()功能,您可以使用CSS :not()僞類根據自己的類來過濾掉某些<div>元素:

$("div.content-wrap div:not(.normalCheck) input[type='checkbox']") 

如果由於某種原因你不希望使用假-class,你仍然可以在jQuery的同樣的事情$.find()

$("div.content-wrap > div").not("div.normalCheck").find("input[type='checkbox']") 

JSFiddle

0

這是您需要的js代碼

$('.content-wrap,.profile-acct-btn').find('input[type="checkbox"],input[type="radio"]').not('.normalCheck input').each(function() { alert($(this).attr('id')); }); 
相關問題