2011-09-05 50 views
0
<div id="customerServices"> 
     <div id="pnlCustomerServices_Container"> 
     <!-- and many others divs...... --> 
     <div id="s1_Container"> 
     <div id="ext-gen32"> 
      <!-- and many others divs........ --> 
      <input type="checkbox" id="s1" 
       name="s1" class=" x-form-checkbox and_many_classes..... parent" value="s1"> 
      <label for="s1" class="x-form-cb-label font-bold" 
       id="ext-gen33">Label1Text</label> 
      <!-- and many others divs........ ---> 
      </div> 
     </div> 

<div id="s2_Container"> 
     <div id="ext-gen33"> 
      <!-- and many others divs........ --> 
      <input type="checkbox" id="s2" 
       name="s2" class=" x-form-checkbox and_many_classes..... parent" value="s2"> 
      <label for="s2" class="x-form-cb-label font-bold" 
       id="ext-gen34">Label1Text</label> 
      <!-- and many others divs........ ---> 
      </div> 
     </div> 

     </div> 
    </div> 

我想找到所有input checkBox(在這種情況下input checkBox id="s1"input checkBox id="s2")有屬性類包含「父」 和嵌套div id="customerServices"查找深層嵌套的輸入的jQuery

但是難以知道customerServicesinput class = "parent"之間有多少個div以及有多少個值具有輸入的屬性類。

更新: 前段時間我使用下面的代碼。但它不適合當前的任務。

$('#customerServices input:checkbox[class="parent"]'); 

UPDATE2: 還有一件事。我怎樣才能找到它沒有屬性class=parent

$('#customerServices input:checkbox[class^="parent"]') 
+2

'$(「input:checkbox.parent」)''有什麼問題嗎? – Jon

回答

1

的複選框,這有什麼錯:

$('.parent') 

$('input .parent') 

要將更新:

$('#customerServices input:checkbox[class="parent"]'); 

選擇class等於「父母」的複選框。但是你的類屬性包含更多。您需要:

$('#customerServices input:checkbox.parent'); 

更新2:

$('#customerServices input:checkbox:not(.parent)'); 
1

你應該能夠

$("input:checkbox.parent") 

選擇您想要的元素存在的捲入,沒有立即明顯的原因<div id="customerServices">,但如果你想減少DOM遍歷,你可以使用更嚴格的

$("#customerServices input:checkbox.parent") 

最後,你以前的

$('#customerServices input:checkbox[class="parent"]') 

解決方案不起作用,因爲它只會選擇元素只有parent類,而不是那些有額外的類。由於具有這種確切的要求是非常不尋常的(我從來沒有在野外看到過),恕我直言,像這樣的選擇器應該被視爲一個錯誤(即使它在寫入時仍然有效)。

1

您的代碼:

$('#customerServices input:checkbox[class="parent"]'); 

因爲你使用了「attribute equals」選擇器,它會尋找匹配class屬性的整個價值不工作(和你有多個類名上市,它會看起來匹配整個字符串)。

但是,class selector,.將匹配單個類名稱。因此,您可以使用類別選擇器,而不是屬性選擇器,它應該工作:

$('#customerServices input:checkbox.parent');