0
有幾個像這樣的HTML片段。在「check_all」點擊時,我想檢查屬於這個「shopcart-con」類的「check_item」。但是當我點擊「check_all」時,所有的「check_item」都被選中了。如何解決它?由於如何讓特定父母的孩子?
HTML:
<div class="shopcart-con" data-merchant-id="1">
<div class="shopcart-list">
<div class="shopcart-lstop"><input type="checkbox" class="check_all" name="select"/>fruit shop A</div>
<div class="shopcart-lscon">
<ul class="shop_contentul" data-product-id="1">
<li class="checkinp"><input type="checkbox" class="check_item" name="aihao" /></li>
<li class="checkimg"><img src="images/home10.png" width="100%" height="75px"></li>
<li class="checknum" >
<p class="lineheight30">apple</p>
<span class="shopcart-num">
<input type="button" name="" class="inp-reduce">
<input type="text" name="" class="num" value="5">
<input type="button" name="" class="inp-add">
</span>
</li>
¥<span class="unit_price">8.00</span>
<p><font color="#808080" size="2px"><s> $12.00</s></font></p>
<p>x<label class="shuli">5</label></p>
<input class="delbtn" name="">
</ul>
<div class="clear"></div>
</div>
<div class="shopcart-lscon">
<ul class="shop_contentul" data-product-id="2">
<li class="checkinp"><input type="checkbox" class="check_item" name="aihao" /></li>
<li class="checkimg"><img src="images/home10.png" width="100%" height="75px"></li>
<li class="checknum" >
<span class="shopcart-num">
<input type="button" name="" class="inp-reduce">
<input type="text" name="" class="num" value="5">
<input type="button" name="" class="inp-add">
</span>
</li>
¥<span class="unit_price">8.00</span>
<!-- <p><font color="#808080" size="2px"><s> $12.00</s></font></p> -->
<p>x<label class="shuli">5</label></p>
<input class="delbtn" name="">
</ul>
<div class="clear"></div>
</div>
<div>
<p class="shopcart-money">共計<label class="product_quantity" data-merchant-id="1">10</label> sum:$<label class="price_sum" data-merchant-id="1" style="color:red">80</label></p>
</div>
</div>
</div>
JS:
$('.check_all').click(function(){
var checked_items = $('.check_all').closest('.shopcart-con').find('.check_item')
console.log(checked_items.length)
if($(this).prop('checked')){
$.each(checked_items, function(i,val){
val.checked = true;
});
}else{
$.each(checked_items, function(i,val){
val.checked = false;
});
}
});
它的工作原理。謝謝 – user1179442 2014-11-04 06:42:44
高興地幫助你:) – 2014-11-04 06:44:54