2016-06-23 94 views
0

我要檢查,如果特定元素的所有複選框被選中,但最後一個 我有這樣的HTML的選擇除了最後一個特定元素的

<ul class="progress-indicator"> 
    <li> 
    //Others elements (variable) 
    <input type="checkbox"> 
    <input type="checkbox"> 
    <input type="checkbox"> 
    <input type="checkbox"> // Exclude this one 
    </li> 
    <li> 
    //Stuff here 
    </li> 
    <li> 
    //Another stuff 
    </li> 
</ul> 

我首先嚐試了

所有複選框
if($(".progress-indicator > li:first-child").find(":checkbox").length == $(".progress-indicator > li:first-child").find(":checkbox:checked").length) 
{ 
    //All checkbox of first li are checked 
} 

這是工作,但現在我要排除的第一li

最後一個複選框,所以我就開始與

$(".progress-indicator > li:first-child").find(":checkbox:not(:last-child)").length 

但它沒有奏效。該選擇器僅選擇第一個li的第一個複選框。我認爲這是由於我以前的Others elements。 (複選框是由div這是另一種元素內內爲例。

那麼,如何選擇第一li的所有複選框除了最後一個複選框li

+2

可能重複的[jquery選擇除最後一個組的所有元素](http://stackoverflow.com/questions/2401109/jquery-selecting-all-elements-except-the-last-per-group) –

回答

0

做的,

var last = $(".progress-indicator >li:first-child").find(":checkbox:last"); 

所以你的聲明將是

if($(".progress-indicator > li:first-child").find(":checkbox").not(last).length == $(".progress-indicator > li:first-child").find(":checkbox:checked").not(last).length) 
{ 
    //All checkbox of first li are checked, regardless of the last one 
} 
0

您能給我們Ëeq用負數& not選擇省略最後一個元素

var x=$("input:checkbox").not(":eq(-1)"); 
x.each(function(){ // this part just for demo 
$(this).prop("checked",true) 
}) 

JSFIDDLE

0

我會使用jQuery的每個功能通過複選框進行迭代。我會排除最後一個。

事情是這樣的:

$checkboxes = $('.checkbox') 
 
lengthCheckboxes = $checkboxes.length; 
 

 
$checkboxes.each(function(i,e) { 
 
\t if (i !== lengthCheckboxes - 1) 
 
\t \t $(e).prop("checked", true); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<li> 
 
    //Others elements (variable) 
 
    <input class="checkbox" type="checkbox"> 
 
    <input class="checkbox" type="checkbox"> 
 
    <input class="checkbox" type="checkbox"> 
 
    <input class="checkbox" type="checkbox"> // Exclude this one 
 
    </li>

0

這應做的伎倆。

$('.progress-indicator input[type="checkbox"]').not(':last-child').each(function(){ 
    $(this).prop("checked", true); 
}); 

親切的問候。

相關問題