2011-09-05 31 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');